Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

simple question about printing vars

by httpd (Novice)
on Nov 03, 2011 at 12:35 UTC ( [id://935649]=perlquestion: print w/replies, xml ) Need Help??

httpd has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

that's just a simple question, but I cannot find a way to operate with data in var, for example, what I can do using opened FILE Descriptor:

open(query, "netstat -na | awk '{print \$4}' | sort -nr |") || die "fi +le not found!"; while(<query>) { ($num, $ip) = split(' '); print "output: $num $ip\n"; } close(query);

How can I do the same with using VAR instead of FILE descriptor? with some thing like this:

$query = `netstat -ntu | awk '{print \$4}' | sort -nr`; while(<$query>) { ($num, $ip) = split(' '); print "output: $num $ip\n"; }
However this "while" construction does not work, can you please help me? may be I should use "foreach" or something else?

Replies are listed 'Best First'.
Re: simple question about printing vars
by moritz (Cardinal) on Nov 03, 2011 at 12:45 UTC

    Backticks just return a string, no magic involved. You can't readline (which is what <$var> does) from a string.

    A possible solution is to open an in-memory file handle:

    open my $handle, '<', \$query or die "Cannot open in-memory file handl +e: $!"; while (<$handle>) { ... }

    Or you can use the backticks in list context:

    for (`...`) { }

    Though for this particular problem, the pipe open is still the best (and least memory hungry) solution.

      thanks, I will use pipe then.
Re: simple question about printing vars
by choroba (Cardinal) on Nov 03, 2011 at 12:43 UTC
    Use reference as the filename:
    $query = `netstat -ntu | awk '{print \$4}' | sort -nr`; open my $FH, '<', \$query; while (<$FH>) { ... }
    Also, consider doing the awk's work and sorting in Perl, too.

      thanks, so as far as I understand using FILE descriptor is the best solution, right?

      as for awk, uniq and sort bash commands, what is the equivalent in perl for that functions, or should I use regexp?

      for example: I need to sort IPs from netstat command, look:
      # netstat -ntu | egrep 'udp|tcp' | awk '{print $4}' | sort | uniq -c | + sort -nr 17 24.13.201.116:80 1 44.43.121.146:22

      how can I do the same using just perl functions?

      Is it better to use perl native functions for such purpose instead of calling external shell commands?
        Something like this?
        netstat -ntu | perl -nae 'next unless $F[0] =~ /tcp|udp/; $seen{$F[4]}++ } { @keys = sort { $seen{$b} <=> $seen{$a} } keys %seen; printf "% " . (1 + length($seen{$keys[0]})) . "d %s\n", $seen{$_}, $_ for @keys '
        Maybe like this?
        #!/usr/bin/perl use strict; use warnings; open PO, "netstat -na|" or die "Can't pipe open: $!\n"; my %count; while (<PO>) { next unless /^udp/ or /^tcp/; my @fields = split; # split $_ on whitespace $count{$fields[3]}++; ## next jumps here } close PO; for my $ip (sort keys %count) { print "\t$count{$ip} $ip\n"; }

        Have a nice day, j

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://935649]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found