Thank you all for your help!

I wanted to share the final script (at least at its current state) and get some feedback from you if at all possible. You can probably tell that I'm more used to writing Bash scripts, I'm sure I'm breaking a few Perl's style guidelines but here it goes. The goal

#!/bin/perl use v5.36; use strict; use warnings; use Getopt::Long qw/GetOptions/; my %files_with_matches = (); my %options = ( 'line_numbers' => 0, 'files_with_matches' => 0, 'print_filename' => -1, ); sub parse_args { GetOptions( 'line-numbers' => \$options{line_numbers}, 'files-with-matches' => \$options{files_with_matches}, 'print-filename!' => \$options{print_filename}, ); if (!@ARGV or $ARGV[0] eq "-") { $options{line_numbers} = 0; $options{files_with_matches} = 0; $options{print_filename} = 0; } else { my $total_files = scalar @ARGV; # -1 indicates this option was not modified. Fallback to # default values depending on how many files are provided. if ($options{print_filename} == -1) { $options{print_filename} = $total_files == 1 ? 0 : 1; } } } # see: https://www.rfc-editor.org/rfc/rfc3986#appendix-A my $hexdig = qr/[a-f0-9]/i; my $pct_encoded = qr/%${hexdig}{2}/; my $unreserved = qr/[a-z0-9\-\._~]/i; my $sub_delims = qr/[!\$&'\(\)\*\+,;=]/; my $pchar = qr/(${unreserved}|${pct_encoded}|${sub_delims}|:|\@)/n; my $dec_oct = qr((\d)|(\d\d)|(1\d\d)|(2[0-4]\d)|(25[0-5]))n; my $ipv4 = qr(${dec_oct}\.${dec_oct}\.${dec_oct}\.${dec_oct}); my $h16 = qr(${hexdig}{1,4}); my $ls32 = qr((${h16} : ${h16}) | ${ipv4})xn; my $ipv6 = qr( ( (${h16} :){6} ${ls32}) | ( :: (${h16} :){5} ${ls32}) | (( ${h16})? :: (${h16} :){4} ${ls32}) | (((${h16} :){0,1} ${h16})? :: (${h16} :){3} ${ls32}) | (((${h16} :){0,2} ${h16})? :: (${h16} :){2} ${ls32}) | (((${h16} :){0,3} ${h16})? :: (${h16} :){1} ${ls32}) | (((${h16} :){0,4} ${h16})? :: ${ls32}) | (((${h16} :){0,5} ${h16})? :: ${h16} ) | (((${h16} :){0,6} ${h16})? :: ) )xn; my $ipvf = qr/v${hexdig}{1,}\.(${unreserved}|${sub_delims}|:){1,}/xn; my $ip_literal = qr{\[(${ipv6}|${ipvf})\]}n; my $re = qr( \b( ([a-z][a-z0-9+-.]*) :// ( ( (${unreserved}|${pct_encoded}|${sub_delims}|:)*@ )? ( ${ip_literal} | ${ipv4} | (${unreserved}|${pct_encoded}|${sub_delims})* ) (:\d+)? ) ( (\/ ( ${pchar}+(\/ ${pchar}*)*)?) | (\/ ${pchar}*)* | (${pchar}+(\/ ${pchar}*)*) | (${pchar}{0}) ) (\?(${pchar}|\/|\?)*)? (\#(${pchar}|\/|\?)*)? )\b )xin; sub match_url { my $row = shift; my $file = shift; while ($row =~ /$re/g) { if ($options{files_with_matches}) { if (! $files_with_matches{$file}) { $files_with_matches{$file} = 1; say $file; } return; } print "$file " if $options{print_filename}; print "$. " if $options{line_numbers}; say "$&"; } } sub main { parse_args(); if (!@ARGV or $ARGV[0] eq "-") { chomp(@ARGV = <STDIN>); match_url($_) for @ARGV; return; } # I went back to this form instead of while(<>) as I wanted to # provide the filename to the match_url subroutine. This was the # only way I knew how to do that. foreach (@ARGV) { open my $fh, '<', $_ or die("Error opening file $_ ($!)\n"); while (my $row = <$fh>) { chomp $row; match_url($row, $_); } close($fh); } } main();
EDIT: Fixed the issue, script works now perfectly (as far as I can tell anyway :D)

In reply to Re: Use global flag when declaring regular expressions with qr? by unmatched
in thread Use global flag when declaring regular expressions with qr? by unmatched

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.