in reply to arch linux small app in perl

#!/usr/bin/perl -w

The first line begins with a space character which means that this will not work properly on Linux unless explicitly called by perl on the command line.    To run this as a stand-alone program the first two characters must be '#!'.

Also, most modern perl programs use the warnings pragma instead of the -w command line switch.    See: perllexwarn for details.



my $scanned = `iwlist scan`; ... my @lines = split(/\n/, $scanned);

Could also be written as:

chomp( my @lines = `iwlist scan` );

But if you want better error information you might want to use open instead.



if ( $pat =~ m/"/i ) {

The /i option is for a case insensitive match but as far as I know the character '"' does not have different upper and lower versions.



if ( $_ =~ m/ttl/i || $_ =~ m/rtt/i) {

That could be more concisely written as:

if ( /ttl|rtt/i ) {


#trims all quotes from the ESSID string sub trim { my @tr = @_; my @trimmed = split(/"/, $tr[0]); return @trimmed; } ... my $connection = &trim($conn[0]);

trim() returns an array, and an array in scalar context returns the number of elements in the array:

$ perl -le' sub test { my @array = "a" .. "z"; # 26 elements return @array; } my $stuff = test(); print $stuff; ' 26

So your $connection variable will always contain a numerical value.    Perhaps you want something like this instead:

( my $connection = $conn[ 0 ] ) =~ tr/"//d;


Please select network by typing it`s name

"it`s" is the contraction of "it is", the possessive form does not have an apostrophe.



The use of an ampersand in subroutine calls is Perl4 style, it is not usually used in Perl5.

Some uses of @array, and then just using $array[0], can be simplified by just using a single scalar instead.

The use of a single string with system might be better with a list instead.

Replies are listed 'Best First'.
Re^2: arch linux small app in perl
by heatblazer (Scribe) on Feb 27, 2012 at 15:59 UTC

    OK, I`ll fix it. The question is how the statement:

     return @some_array;

    would not return all array values like return $a, $b $c... etc? My trim() returned the names without the ' " '. I am still getting used to perl`s syntax since I came from JS/HTML/C. I`ll make it more readable in the future.

    I got the error!

    my @connection = &trim($conn[0]); if (system("iwconfig wlan0 essid $connection[0] key s:$pawd " +) ) { return 1; }

    This should fix it, right?

      The question is how the statement:
      return @some_array;
      would not return all array values like return $a, $b $c... etc?

      Because the subroutine is called in SCALAR context and the context affects the return value(s).    A list (like $a, $b, $c) called in SCALAR context would return the last element of the list.    You can use the wantarray operator to determine what context the subroutine was called in.



      I got the error!

      What error did you get?



      my @connection = &trim($conn[0]); if (system("iwconfig wlan0 essid $connection[0] key s:$pawd " ) ) {

      Now you are calling trim in LIST context but you appear to only need the first element of the returned list.    Perhaps you should only return the first element if that is all you require:

      sub trim { my ( $tr ) = @_; return ( split /"/, $tr )[ 0 ]; }