[32953] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4229 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jun 2 16:09:20 2014

Date: Mon, 2 Jun 2014 13:09:05 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 2 Jun 2014     Volume: 11 Number: 4229

Today's topics:
        best way to build an absolute spec oldyork90@yahoo.com
    Re: best way to build an absolute spec <jurgenex@hotmail.com>
    Re: best way to build an absolute spec oldyork90@yahoo.com
    Re: best way to build an absolute spec <rweikusat@mobileactivedefense.com>
    Re: Delicious Perl bug (Tim McDaniel)
    Re: Delicious Perl bug <rweikusat@mobileactivedefense.com>
    Re: Help with an operator precedence (?) puzzle <rm-dash-bau-haus@dash.futureapps.de>
    Re: Help with an operator precedence (?) puzzle <rm-dash-bau-haus@dash.futureapps.de>
        process multiple hashes <juliani.moon@gmail.com>
    Re: sorting file according to a unicode column <gravitalsun@hotmail.foo>
    Re: sorting file according to a unicode column <gravitalsun@hotmail.foo>
    Re: sorting file according to a unicode column <gravitalsun@hotmail.foo>
    Re: sorting file according to a unicode column ehabaziz2001@gmail.com
    Re: sorting file according to a unicode column ehabaziz2001@gmail.com
    Re: sorting file according to a unicode column <rweikusat@mobileactivedefense.com>
        using GetOpt in a sub oldyork90@yahoo.com
    Re: using GetOpt in a sub <news@todbe.com>
    Re: using GetOpt in a sub (Tim McDaniel)
    Re: using GetOpt in a sub <rweikusat@mobileactivedefense.com>
    Re: using GetOpt in a sub <rweikusat@mobileactivedefense.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sun, 1 Jun 2014 10:34:49 -0700 (PDT)
From: oldyork90@yahoo.com
Subject: best way to build an absolute spec
Message-Id: <466879eb-ff22-41bc-8d3e-1c8b039eb333@googlegroups.com>

I have a directory $my_dir. I test this with -d. 
$my_dir is user defined.

I want to build the file spec "$my_dir . $my_fn", but $my_dir may be 
constructed as either /one/two or /one/two/.

I could test for the trailing path separator and act accordingly, but is
there a better way, a "standard" or best practice way, of doing this?  

How do you pros build the absolute paths?


I find that in Windows "/one/two//my_file" will work, but I'd rather not.

Thanks in advance.


------------------------------

Date: Sun, 01 Jun 2014 10:51:36 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: best way to build an absolute spec
Message-Id: <rspmo9tjp8s046rvfusniibfimsm3s6j8d@4ax.com>

oldyork90@yahoo.com wrote:
>I have a directory $my_dir. I test this with -d. 
>$my_dir is user defined.
>
>I want to build the file spec "$my_dir . $my_fn", but $my_dir may be 
>constructed as either /one/two or /one/two/.
>
>I could test for the trailing path separator and act accordingly, but is
>there a better way, a "standard" or best practice way, of doing this?  
>
>How do you pros build the absolute paths?

By using one of the several modules that do that job, e.g.
http://search.cpan.org/~smueller/PathTools-3.47/lib/File/Spec.pm
http://search.cpan.org/~dagolden/Path-Tiny-0.054/lib/Path/Tiny.pm
or several others

jue


------------------------------

Date: Sun, 1 Jun 2014 11:09:01 -0700 (PDT)
From: oldyork90@yahoo.com
Subject: Re: best way to build an absolute spec
Message-Id: <def8ebf8-7c70-4b07-bfc7-11e65010c9e5@googlegroups.com>


> >How do you pros build the absolute paths?
> 
> 
> 
> By using one of the several modules that do that job, e.g.
> 
> http://search.cpan.org/~smueller/PathTools-3.47/lib/File/Spec.pm
> 
> http://search.cpan.org/~dagolden/Path-Tiny-0.054/lib/Path/Tiny.pm
> 
> or several others
> 
> 
> 
> jue

   my $fspec = File::Spec->catfile ($my_dir, $fn);

Works great.  Thanks.  (changes / to \... hope it doesn't cause any problems.
I grew up with \ as an escape.)


------------------------------

Date: Sun, 01 Jun 2014 21:02:37 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: best way to build an absolute spec
Message-Id: <8761kkl7oi.fsf@sable.mobileactivedefense.com>

oldyork90@yahoo.com writes:
> I have a directory $my_dir. I test this with -d. 
> $my_dir is user defined.
>
> I want to build the file spec "$my_dir . $my_fn", but $my_dir may be 
> constructed as either /one/two or /one/two/.
>
> I could test for the trailing path separator and act accordingly, but is
> there a better way, a "standard" or best practice way, of doing this?  
>
> How do you pros build the absolute paths?
>
>
> I find that in Windows "/one/two//my_file" will work, but I'd rather
> not.

POSIX/ UNIX(*) pathnames may separate two components with any number of
/-characters (subject to pathname lenght limits).


------------------------------

Date: Mon, 2 Jun 2014 05:40:55 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Delicious Perl bug
Message-Id: <lmh2p7$711$1@reader1.panix.com>

In article <lmevks$aa2$1@dont-email.me>,
Ilya Zakharevich  <nospam-abuse@ilyaz.org> wrote:
>        perl -wlpe "s[\w]"

You were being way too cute in being too brief and not describing what
you are thinking.

perl
-w warnings
-l enables automatic line-ending processing: chomp() and set $\
-p autoprint the result
-e the program is the next argument

s[\w]: it took me a moment to realize that you're expecting it to
cause an error about a bad s///, but it works as a substitute.

$ perl -wlpe 's[\w]; FROG  FROG'
s3333
 FROG  FROG
 3333

But it works only with ;.  Aha!

$ perl -wlpe 's[\w]! FROG  FROG'
Backslash found where operator expected at -e line 2, near
";}continue{print or die qq(-p destination: $!\"
        (Missing operator before \?)
Unquoted string "n" may clash with future reserved word at -e
line 2.
Final $ should be \$ or $name at -e line 1, within string
syntax error at -e line 2, near ";}continue{print or die qq(-p
destination: $!"
  (Might be a runaway multi-line !! string starting on line 1)
Execution of -e aborted due to compilation errors.

Apparently, where "man perlrun" says

-p   causes Perl to assume the following loop around your program, which
     makes it iterate over filename arguments somewhat like sed:

         LINE:
           while (<>) {
               ... # your program goes here
           } continue {
               print or die "-p destination: $!\n";
           }

it doesn't mean "it'll be treated like that", it means "it bloody well
IS wrapped in that, in a very real and literal sense".  Similarly

$ perl -wlpe 's[\w]{ FROG  FROG'
Having no space between pattern and following word is deprecated at -e
line 2.
Use of /c modifier is meaningless in s/// at -e line 2.
Bareword found where operator expected at -e line 2, near ";}continue"
  (Might be a runaway multi-line {} string starting on line 1)
        (Missing operator before ntinue?)
syntax error at -e line 2, near ";}continue"
Missing right curly or square bracket at -e line 2, at end of line
Execution of -e aborted due to compilation errors.

Is there any way to fix this in general?  That is, is there any way to
have an arbitrary set of Perl code and interpolate it into a code
skeleton where the interpolation can't break the skeleton somehow?
          
-- 
Tim McDaniel, tmcd@panix.com


------------------------------

Date: Mon, 02 Jun 2014 12:54:57 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Delicious Perl bug
Message-Id: <87k38zwmpa.fsf@sable.mobileactivedefense.com>

tmcd@panix.com (Tim McDaniel) writes:
> In article <lmevks$aa2$1@dont-email.me>,
> Ilya Zakharevich  <nospam-abuse@ilyaz.org> wrote:
>>        perl -wlpe "s[\w]"

[...]

> Apparently, where "man perlrun" says
>
> -p   causes Perl to assume the following loop around your program, which
>      makes it iterate over filename arguments somewhat like sed:
>
>          LINE:
>            while (<>) {
>                ... # your program goes here
>            } continue {
>                print or die "-p destination: $!\n";
>            }
>
> it doesn't mean "it'll be treated like that", it means "it bloody well
> IS wrapped in that, in a very real and literal sense".

The documentation actually says so.

[...]

> Is there any way to fix this in general?

The good question is "What is 'fix' supposed to mean here?", eg this
code

perl  -lpe '} while (1) { ++$_'

behaves exactly like the documentation says it would (when running it
without an input file, it will 'read something' from stdin and then
print an endless sequence of ascending numbers).


------------------------------

Date: Mon, 02 Jun 2014 14:16:20 +0200
From: "G.B." <rm-dash-bau-haus@dash.futureapps.de>
Subject: Re: Help with an operator precedence (?) puzzle
Message-Id: <538c6b13$0$6617$9b4e6d93@newsspool4.arcor-online.net>

On 30.05.14 18:54, Rainer Weikusat wrote:
>> What if a certain combination of syntactic features of Perl is
>> >beyond the ability of a programmer .IFF. the programmer cannot
>> >parse correctly a line exhibiting the feature, in under 60 seconds?

> That's a rethorical question without any substance behind it.

No, since to ask about in situations in which some piece of Perl
is puzzling or not, *and* to indicate one potential criterion
is simply to ask for observable facts.



------------------------------

Date: Mon, 02 Jun 2014 14:21:13 +0200
From: "G.B." <rm-dash-bau-haus@dash.futureapps.de>
Subject: Re: Help with an operator precedence (?) puzzle
Message-Id: <538c6c38$0$6615$9b4e6d93@newsspool4.arcor-online.net>

On 30.05.14 18:54, Rainer Weikusat wrote:
>> And again ... I'm pointing to evidence of "elementary" features
>> >of Perl being puzzling. Not puzzling you, but others, obviously.
> That's another assertion that elementary features of Perl (regarded as
> 'elementary' because they're described on the first two pages of the
> relevant manpage) are surely "puzzling".
>

Again, "relevant"?  How so? I'd be interested in evidence
that Perl programmers learn Perl from man pages that serve
the same purpose that the C standard documents serve in
the case of C. (Thus, by analogy, about C programmers who
learn C from the standard.)

It is essential, too, that "elementary" (cf. elementary school)
does not fairly describe the man pages you have mentioned (the
reference material, I'd rather call them), if one wants to know
just how puzzling or not some Perl statements can be for readers
of Perl programs.



------------------------------

Date: Mon, 2 Jun 2014 12:41:30 -0700 (PDT)
From: Joe <juliani.moon@gmail.com>
Subject: process multiple hashes
Message-Id: <978f2d62-bfd0-4e15-a67e-43fff8fd4d59@googlegroups.com>

This may be naive, but here is a situation:

I have a number of hashes stored in a config file, each contains different =
set of data and they all have the same data structure and will be processed=
 in the same way. I want to build a loop to process them one at a time. Now=
 my question is, is there a way for me to get all there hash names (as hash=
 reference I guess), put them in an array so I can build a 'foreach' to pro=
cess them? (I have all hash names share something in common, like %AAlevel,=
 %BBlevel, %CClevel, etc., and naively wished to grab them by REGEX :)

I did some search and didn't seem to find anything close, and hope to have =
a luck here. Thanks in advance,

joe




------------------------------

Date: Mon, 02 Jun 2014 14:01:07 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: sorting file according to a unicode column
Message-Id: <lmhlgf$18j8$1@news.ntua.gr>

If you are on a proper installed linux system and your files are already 
valid utf8, then you do not need the definitions
#   open     FILE, '>:utf8',
#   binmode STDOUT, ':utf8';
based on the sample line, the following code will do the job






#!/usr/bin/perl
use strict;
use warnings;



my $column       = 9; # use this column for sorting
my $offset_start = 29; # importand position of the column start (count 
from 0)
my $offset_end   = 60; # importand position of the column end   (count 
from 0)
my @lines;


open FILE, '<', '/tmp/file1.txt' or die "oh no ! \"$!\"\n";

while (<FILE>) {
chomp;
my $order_by = (split /\|/, $_, -1)[$column];
my $Length   = length $order_by;
my $offend   = $offset_end > $Length ? $Length : $offset_end;
$order_by    = substr $order_by, $offset_start, ($offset_end-$offset_start);
push @lines, [$order_by, $_]
}

close FILE;

# use Data::Dumper; print Dumper \@lines;

foreach (sort { $a->[0] cmp $b->[0] } @lines) {
print "$_->[1]\n"
}



------------------------------

Date: Mon, 02 Jun 2014 14:02:59 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: sorting file according to a unicode column
Message-Id: <lmhljv$18j8$2@news.ntua.gr>

there was a type at the code, here is the correct


#!/usr/bin/perl
use strict;
use warnings;



# http://unicode.org/charts/PDF/U0600.pdf

# 1,3      29

my $column       = 8; # use this column for sorting
my $offset_start = 1; # importand position of the column start (count 
from 0)
my $offset_end   = 3; # importand position of the column end   (count 
from 0)
my @lines;


open FILE, '<', '/tmp/file1.txt' or die "oh no ! \"$!\"\n";

while (<FILE>) {
chomp;
my $order_by = (split /\|/, $_, -1)[$column];
my $Length   = length $order_by;
my $offend   = $offset_end > $Length ? $Length : $offset_end;
$order_by    = substr $order_by, $offset_start, ($offend-$offset_start);
push @lines, [$order_by, $_]
}

close FILE;

# use Data::Dumper; print Dumper \@lines;

foreach (sort { $a->[0] cmp $b->[0] } @lines) {
print "$_->[1]\n"
}



------------------------------

Date: Mon, 02 Jun 2014 14:04:07 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: sorting file according to a unicode column
Message-Id: <lmhlm2$18j8$3@news.ntua.gr>

#!/usr/bin/perl
use strict;
use warnings;

my $column       = 9; # use this column for sorting
my $offset_start = 29; # importand position of the column start (count 
from 0)
my $offset_end   = 50; # importand position of the column end   (count 
from 0)
my @lines;

open FILE, '<', '/tmp/file1.txt' or die "oh no ! \"$!\"\n";

while (<FILE>) {
chomp;
my $order_by = (split /\|/, $_, -1)[$column];
my $Length   = length $order_by;
my $offend   = $offset_end > $Length ? $Length : $offset_end;
$order_by    = substr $order_by, $offset_start, ($offend-$offset_start);
push @lines, [$order_by, $_]
}

close FILE;

foreach (sort { $a->[0] cmp $b->[0] } @lines) {
print "$_->[1]\n"
}



------------------------------

Date: Mon, 2 Jun 2014 12:08:48 -0700 (PDT)
From: ehabaziz2001@gmail.com
Subject: Re: sorting file according to a unicode column
Message-Id: <465b4f93-8300-45e1-bba4-507e231ecd5f@googlegroups.com>

I am running perl under XP dos box. 
------------------------------------
I got that errors after running :
F:\COMPILER\Perl\bin\perl sorting.pl 1.log

Here is my script:
-----------------

use strict; 
use warnings; 

my $column       = 2; # use this column for sorting 
my $offset_start = 9; # importand position of the column start (count 
from 0) 
my $offset_end   = 209; # importand position of the column end   (count 
from 0) 
my @lines; 

open FILE, '<', 'F:\programs\sedawk\xerox_scripts\AlexBank\scripts\1.log' or die "oh no ! \"$!\"\n"; 

while (<FILE>) { 
chomp; 
my $order_by = (split /\|/, $_, -1)[$column]; 
my $Length   = length $order_by; 
my $offend   = $offset_end > $Length ? $Length : $offset_end; 
$order_by    = substr $order_by, $offset_start, ($offend-$offset_start); 
push @lines, [$order_by, $_] 
} 

close FILE; 

The error :
-----------
Number found where operator expected at sorting.pl line 6, near "from 0"
        (Do you need to predeclare from?)
Number found where operator expected at sorting.pl line 8, near "from 0"
        (Do you need to predeclare from?)
syntax error at sorting.pl line 6, near "from 0"
Can't use global $! in "my" at sorting.pl line 11, near "oh no ! "$!"
Execution of sorting.pl aborted due to compilation errors.
F:\programs\sedawk\xerox_scripts\AlexBank\scripts>


------------------------------

Date: Mon, 2 Jun 2014 12:13:23 -0700 (PDT)
From: ehabaziz2001@gmail.com
Subject: Re: sorting file according to a unicode column
Message-Id: <e6f02a10-05dd-46e4-a580-511ee15cdb6a@googlegroups.com>

Please I can not see any output with the below command  :
F:\COMPILER\Perl\bin\perl sorting.pl 1.log 


On Wednesday, May 28, 2014 4:22:10 PM UTC+2, ehabaz...@gmail.com wrote:
> Dear All,
> 
> I need to use that below script but with no success . Even I try to use the ucsort script but also with no success . Any help
> 
> The files columns separated with '|' and the column position 9 with length 200.
> 
> 
> 
> use encoding 'utf8'; 
> 
> my @File; 
> 
> my %Positions_and_lenghts = ( 
> 
> 9 => 180 , 
> 
> ); 
> 
> 
> 
> 
> 
> 
> 
> #For external file 
> 
> open FILE, '>:utf8', 'F:\programs\sedawk\xerox_scripts\AlexBank\logs\all_cycle_data.sorted' or die "$^E\n"; 
> 
> binmode STDOUT, ':utf8'; 
> 
> 
> 
> 
> 
> while (my $line = <DATA>) { 
> 
> my $row; 
> 
>     foreach my $POS (sort {$a<=>$b} keys %Positions_and_lenghts) { 
> 
>     push @{$row}, substr $line, $POS, $Positions_and_lenghts{$POS} 
> 
>     } 
> 
> push @File, $row 
> 
> 
> 
> 
> 
> } 
> 
> 
> 
> 
> 
> #use Data::Dumper; print Dumper(\@File);exit; 
> 
> 
> 
> foreach my $row (sort {$a->[0] cmp $b->[0] || $a->[2] cmp $b->[2]} @File) { 
> 
> print "@{$row}\n" 
> 
> 
> 
> 
> 
> 
> 
> }



------------------------------

Date: Mon, 02 Jun 2014 20:13:37 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: sorting file according to a unicode column
Message-Id: <87zjhvf7ku.fsf@sable.mobileactivedefense.com>

ehabaziz2001@gmail.com writes:
> I am running perl under XP dos box. 
> ------------------------------------
> I got that errors after running :
> F:\COMPILER\Perl\bin\perl sorting.pl 1.log

[...]

> my $offset_start = 9; # importand position of the column start (count 
> from 0)

> my $offset_end   = 209; # importand position of the column end   (count 
> from 0)

The 'from 0)' belongs to the comment started the respective lines
above. It must not appear alone on a line, as it does here.


------------------------------

Date: Sun, 1 Jun 2014 14:56:03 -0700 (PDT)
From: oldyork90@yahoo.com
Subject: using GetOpt in a sub
Message-Id: <cfd3df2f-12cf-42a9-9d92-77925015a2d0@googlegroups.com>

Is this done?

Instead of calling a sub with a defined set of parameters, call it instead with a single string.  This string is tokenized and then GetOpt called to sort out the mess.

As some of my sub get "optiony" I see a use for this construct.  While I should probably create new subs for some of the option configurations, I was curious about this use of Getopt.

I see Getopt allows for a user's @ARGV.

Always curious about the way the pros do it.

Thanks in advance.



------------------------------

Date: Sun, 01 Jun 2014 18:55:59 -0700
From: "$Bill" <news@todbe.com>
Subject: Re: using GetOpt in a sub
Message-Id: <lmgljh$2kb$1@dont-email.me>

On 6/1/2014 14:56, oldyork90@yahoo.com wrote:
> Is this done?
>
> Instead of calling a sub with a defined set of parameters, call it instead with a single string.  This string is tokenized and then GetOpt called to sort out the mess.
>
> As some of my sub get "optiony" I see a use for this construct.  While I should probably create new subs for some of the option configurations, I was curious about this use of Getopt.
>
> I see Getopt allows for a user's @ARGV.
>
> Always curious about the way the pros do it.

You're more likely to see multiple args passed as a hash instead of a string:

use strict;
use warnings;

do_it ('x' => 1, 'y' => 2);

sub do_it {
	my %h = @_;
printf "x=%s y=%s\n",  $h{'x'}, $h{'y'};
}

__END__



------------------------------

Date: Mon, 2 Jun 2014 05:04:00 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: using GetOpt in a sub
Message-Id: <lmh0k0$gm7$1@reader1.panix.com>

In article <lmgljh$2kb$1@dont-email.me>, $Bill <news@todbe.com> wrote:
>You're more likely to see multiple args passed as a hash instead of a string:

I've never seen any getopt-type processing for subs, and I'd strongly
recommend against it.  My company passes hashes for args.

getopt is intended for command lines being typed by the user, so
having a premium on brevity, and they are dynamic and casual and often
complicated.  Code is write once run many, and usually has much
simpler invocation conventions.

I think sub calls are clearer if "preparsed", be it represented by
hash, lists or arrays of arguments, or whatever: the parsing is done
once when writing the code and you don't have to worry about the
correct shortcut for --frobotz or whatever.  It's also much more
efficient, but that's a concern only for the subs called most often.

-- 
Tim McDaniel, tmcd@panix.com


------------------------------

Date: Mon, 02 Jun 2014 13:49:35 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: using GetOpt in a sub
Message-Id: <87y4xfv5ls.fsf@sable.mobileactivedefense.com>

oldyork90@yahoo.com writes:
> Is this done?
>
> Instead of calling a sub with a defined set of parameters, call it
> instead with a single string.  This string is tokenized and then
> GetOpt called to sort out the mess.

Getopt usually works on a list of strings.

> As some of my sub get "optiony" I see a use for this construct.  While
> I should probably create new subs for some of the option
> configurations, I was curious about this use of Getopt.

[...]

> Always curious about the way the pros do it.

This depends somewhat on how subroutines are being used. There are
basically two competing approaches here:

1. "I have to dig a tunnel below Mordor with a teaspoon until tomorrow
so I'd better start shoveling!" aka 'subroutines are not being used'
(except insofar they can be downloaded from the internet free of
charge). That's the traditional way to avoid structuring a program and
much beloved by people with an excellent short-term memory who prefer
to 'keep on shoveling' until the problems runs out instead of designing
a machine which solves the problem. In this case, how subroutine
arguments are dealt with pretty much doesn't matter: There's exactly one
'subroutine' and maybe it had some arguments 6,500 lines in the past but
nobody can remember if it did and in any case we gotta keep on shoveling
instead of being distracted with silly questions like that.

2. Subroutines are being in a way known as 'functional decomposition',
that is, each subroutine corresponds with an 'abstraction layer' and it
is invoked by more abstract subroutines and invokes less abstract
ones. In this case, there'll be many subroutines and they're typically
short (30 lines of code or less). Likewise, parameter list are going be
short (1 - 4 parameters) and arguments will usually be accessed by
position because that's faster and more elaborate schemes aren't
needed. Should an elaborate scheme for dealing with subroutine arguments
be needed, this would indicate that the subroutine in question does too
many different things and should be further decomposed.

I usually prefer 2) becauses this provides a reasonably simple way to
solve complicated problems, however, creating seriously complicated
solutions to simple problems is also very popular ...


------------------------------

Date: Mon, 02 Jun 2014 17:34:17 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: using GetOpt in a sub
Message-Id: <87d2ergtiu.fsf@sable.mobileactivedefense.com>

"$Bill" <news@todbe.com> writes:
> On 6/1/2014 14:56, oldyork90@yahoo.com wrote:
>> Is this done?
>>
>> Instead of calling a sub with a defined set of parameters, call it instead with a single string.  This string is tokenized and then GetOpt called to sort out the mess.
>>
>> As some of my sub get "optiony" I see a use for this construct.  While I should probably create new subs for some of the option configurations, I was curious about this use of Getopt.
>>
>> I see Getopt allows for a user's @ARGV.
>>
>> Always curious about the way the pros do it.
>
> You're more likely to see multiple args passed as a hash instead of a string:
>
> use strict;
> use warnings;
>
> do_it ('x' => 1, 'y' => 2);
>
> sub do_it {
> 	my %h = @_;
> printf "x=%s y=%s\n",  $h{'x'}, $h{'y'};
> }

That's not really passing arguments as a hash, it is equivalent to

do_it('x', 1, 'y', 2)

except that the '' could be omitted when using =>. This four-element
list is then used to initialize a hash and the elements on the list
alternatingly become hash keys and associated values. This is similar to
the way 'command-line options' are usually handled: There's an argument
naming 'the option', followed by an argument giving its value. Unless a
subroutine takes a lot of different arguments in no particular order,
passing arguments by position is more sensible, ie

sub do_it
{
	my ($x, $y) = @_;
}




------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 4229
***************************************


home help back first fref pref prev next nref lref last post