[33107] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4383 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Mar 4 09:09:21 2015

Date: Wed, 4 Mar 2015 06:09:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 4 Mar 2015     Volume: 11 Number: 4383

Today's topics:
    Re: function returning a list of the indices of its tru <rweikusat@mobileactivedefense.com>
    Re: function returning a list of the indices of its tru <gravitalsun@hotmail.foo>
    Re: function returning a list of the indices of its tru <gravitalsun@hotmail.foo>
    Re: function returning a list of the indices of its tru <rweikusat@mobileactivedefense.com>
        Suppressing 5.18 "experimental" warnings without changi <news@lawshouse.org>
    Re: Suppressing 5.18 "experimental" warnings without ch <gravitalsun@hotmail.foo>
        variable expansion issue herbert.burnswell@gmail.com
    Re: variable expansion issue <gravitalsun@hotmail.foo>
    Re: variable expansion issue <jurgenex@hotmail.com>
    Re: variable expansion issue <news@lawshouse.org>
    Re: variable expansion issue herbert.burnswell@gmail.com
    Re: variable expansion issue <gravitalsun@hotmail.foo>
    Re: variable expansion issue <gravitalsun@hotmail.foo>
    Re: variable expansion issue <gravitalsun@hotmail.foo>
    Re: variable expansion issue <news@lawshouse.org>
    Re: variable expansion issue herbert.burnswell@gmail.com
    Re: variable expansion issue (Tim McDaniel)
    Re: variable expansion issue <No-Spam@deezee.org>
    Re: variable expansion issue <rweikusat@mobileactivedefense.com>
    Re: variable expansion issue <rweikusat@mobileactivedefense.com>
    Re: variable expansion issue <rweikusat@mobileactivedefense.com>
    Re: variable expansion issue <justin.1502@purestblue.com>
    Re: variable expansion issue <gravitalsun@hotmail.foo>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 02 Mar 2015 15:44:32 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <87wq2ze74v.fsf@doppelsaurus.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> On 27/2/2015 4:00 μμ, gamo wrote:
>> El 26/02/15 a las 22:31, Rainer Weikusat escribió:
>>> sub trues
>>> {
>>>      map { $_[$_] ? $_ : () } 0 .. $#_;
>>> }
>>>
>>> I actually needed this for something and found it rather amusing.
>
> nothing fancy but anyway
>
>
> sub trues3
> {
> my @B;
>
> 	while (my @A = each @_ )
> 	{
> 	push @B, $A[0] if $A[1]	
> 	}
> @B
> }

There isn't anything fancy about the map (and certainly not about the
grep) expression, either, it's just a functional and not an imperative
solution (OTOH, one can argue that defining a meand for each applied to
an array is a fancy 'People Hate Perl' imitation -- PHP doesn't support
arrays, hence, there's always both a key set and a value set for all
'PHP containers' ...).

Considering that I don't need the indices in order, here's a moderately
more interesting one:

sub trues
{
    @_ ? (pop() ? @_ + 0 : (), &trues) : ();
}

"Once upon a time in the past" I had the mispleasure to make the
aquaintance of a guy who was very fond of teaching functional
programming in introductory university courses because he was convinced
that nobody who ever learnt programming with imperative constructs could
ever get his head around that ...


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

Date: Mon, 02 Mar 2015 20:43:32 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <md2b0k$h13$1@news.ntua.gr>

On 2/3/2015 5:44 μμ, Rainer Weikusat wrote:
> sub trues
> {
>      @_ ? (pop() ? @_ + 0 : (), &trues) : ();
> }

this is cool !


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

Date: Tue, 03 Mar 2015 03:03:53 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <md319n$2pub$1@news.ntua.gr>

On 26/2/2015 11:31 μμ, Rainer Weikusat wrote:
> sub trues
> {
>      map { $_[$_] ? $_ : () } 0 .. $#_;
> }
>
> I actually needed this for something and found it rather amusing.
>

an other


my $data = Constructor('a' , 0 , 'c');

while()
{
my $var = $data->(); $var==-1 && last;
say $var;
}

sub Constructor
{
my @A=@_;
my $i=-1;
sub { { $i++ > $#A ? -1 : $A[$i] ? $i : redo } }
}



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

Date: Tue, 03 Mar 2015 18:25:21 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: function returning a list of the indices of its true arguments
Message-Id: <87d24qc50u.fsf@doppelsaurus.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> On 26/2/2015 11:31 μμ, Rainer Weikusat wrote:
>> sub trues
>> {
>>      map { $_[$_] ? $_ : () } 0 .. $#_;
>> }
>>
>> I actually needed this for something and found it rather amusing.
>>
>
> an other
>
>
> my $data = Constructor('a' , 0 , 'c');
>
> while()
> {
> my $var = $data->(); $var==-1 && last;
> say $var;
> }
>
> sub Constructor
> {
> my @A=@_;
> my $i=-1;
> sub { { $i++ > $#A ? -1 : $A[$i] ? $i : redo } }
> }

The generator wouldn't be useful for me and I'm generally not very fond
of code obfuscation, eg, ?: is something supposed to return a value and
not intended to be a syntactic shortcut for if - else. The same can be
expressed as

sub trues_gen
{
    my @args = @_;
    my $i = -1;
    
    sub {
	$args[++$i] and return $i while $i < @args;
	();
    }
}

or even

sub trues_gen
{
    my @args = @_;
    my $i = -1;
    
    sub {
	$args[++$i] and return $i while $i < @args;
    }
}

if returning something sensible in list context is not desired with much
less "forced ingenuity".


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

Date: Mon, 02 Mar 2015 22:25:04 +0000
From: Henry Law <news@lawshouse.org>
Subject: Suppressing 5.18 "experimental" warnings without changing all my code?
Message-Id: <1rCdnRN4MP3ffmnJnZ2dnUVZ8umdnZ2d@giganews.com>

I have numerous programs and modules which make use of the (what I 
thought were) not-terribly-exotic 5.010 switch features, that is "given" 
and "when".  Having just upgraded to Perl 5.018 I'm suddenly getting 
screeds of warnings about these features being experimental.

Let's not talk about why they're experimental and why the warnings are 
there. And I know how to turn them off in the code (the "experimental" 
pragma is very straightforward).

But is there no way of suppressing these things without editing every 
piece of code to include "use experimental qw(switch)"?  That's a right 
royal pain to do.

-- 

Henry Law            Manchester, England


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

Date: Tue, 03 Mar 2015 00:34:43 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Suppressing 5.18 "experimental" warnings without changing all my code?
Message-Id: <md2oi2$1o0d$1@news.ntua.gr>

On 3/3/2015 12:25 πμ, Henry Law wrote:
> I have numerous programs and modules which make use of the (what I
> thought were) not-terribly-exotic 5.010 switch features, that is "given"
> and "when".  Having just upgraded to Perl 5.018 I'm suddenly getting
> screeds of warnings about these features being experimental.
>
> Let's not talk about why they're experimental and why the warnings are
> there. And I know how to turn them off in the code (the "experimental"
> pragma is very straightforward).
>
> But is there no way of suppressing these things without editing every
> piece of code to include "use experimental qw(switch)"?  That's a right
> royal pain to do.
>

	use strict;
	use warnings;
	no  warnings 'experimental';
	use feature qw/switch say/;



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

Date: Mon, 2 Mar 2015 14:23:24 -0800 (PST)
From: herbert.burnswell@gmail.com
Subject: variable expansion issue
Message-Id: <0e9639cf-c1ab-45df-9bf6-5add29664516@googlegroups.com>

Here is what I have:

my $inst = "$ARGV[0]";
my $root = "/path/${inst}";

my $dir1 = "${root}/path/to/directory
my $dir2 = "${root}/path/to/directory2

my @dirs = qw(${root}/path/to/path1 ${root}/path/to/path2 ${root}/path/to/path3);


Two issues:

	- I get the warning:

		"Use of uninitialized value in string at ./script.pl line 29."

		Which as you can guess is:

		my $inst = "$ARGV[0]";

	- The array @dirs does not get the variable expansion for ${root} but $dir1 and $dir2 work fine.

Is there a way to do what I am trying to do or must I go about it a different way?  Any guidance is greatly appreciated.

TIA,

Herb


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

Date: Tue, 03 Mar 2015 00:30:53 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: variable expansion issue
Message-Id: <md2oas$1n8n$1@news.ntua.gr>

On 3/3/2015 12:23 πμ, herbert.burnswell@gmail.com wrote:
> Is there a way to do what I am trying to do or must I go about it a different way?  Any guidance is greatly appreciated.

# sure why not !

my $inst = $ARGV[0] // 'Moon_base_A';
my $root = "/path/$inst";
my $dir1 = "$root/path/to/directory";
my $dir2 = "$root/path/to/directory2";
my @dirs = ("$root/path/to/path1", "$root/path/to/path2", 
"$root/path/to/path3");
print "$_\n" for @dirs;


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

Date: Mon, 02 Mar 2015 14:42:15 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: variable expansion issue
Message-Id: <1cp9fat24au23df0dpnje6fvvm0gsdqa49@4ax.com>

herbert.burnswell@gmail.com wrote:
>Here is what I have:
>
>my $inst = "$ARGV[0]";

Why are you stringifying a string value?
Those double quotes are completely useless.

>my $root = "/path/${inst}";
>
>my $dir1 = "${root}/path/to/directory
>my $dir2 = "${root}/path/to/directory2
>
>my @dirs = qw(${root}/path/to/path1 ${root}/path/to/path2 ${root}/path/to/path3);
>
>
>Two issues:
>
>	- I get the warning:
>
>		"Use of uninitialized value in string at ./script.pl line 29."

But your script doesn't have 29 lines....

And when I copy-n-paste your code above then I am getting
Scalar found where operator expected at C:\tmp\t.pl line 7, near "my
$dir2 = "${root}"
  (Might be a runaway multi-line "" string starting on line 4)

And after fixing that line and the next one, too, there doesn't seem to
be a problem any longer

>		Which as you can guess is:
>
>		my $inst = "$ARGV[0]";

So, did you pass a command line argument to your script?

>	- The array @dirs does not get the variable expansion for ${root} but $dir1 and $dir2 work fine.
>
>Is there a way to do what I am trying to do or must I go about it a different way?  Any guidance is greatly appreciated.

Please see "perldoc perlop", section "Quote and quote-like operators".
qw// does not interpolate. You will have to choose an operator that does
interpolate.

jue


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

Date: Mon, 02 Mar 2015 22:45:42 +0000
From: Henry Law <news@lawshouse.org>
Subject: Re: variable expansion issue
Message-Id: <k-GdnUKYQsGFdWnJnZ2dnUVZ8tydnZ2d@giganews.com>

On 02/03/15 22:23, herbert.burnswell@gmail.com wrote:
> Here is what I have:

No, it isn't; it's got syntax errors in it, so when I copied it to my 
editor to try to help you the code failed.  The way to avoid this is 
/always/ to copy and paste code when posting here.

>
> my $inst = "$ARGV[0]";
> my $root = "/path/${inst}";
>
> my $dir1 = "${root}/path/to/directory
> my $dir2 = "${root}/path/to/directory2
>
> my @dirs = qw(${root}/path/to/path1 ${root}/path/to/path2 ${root}/path/to/path3);

> 	- I get the warning:
>
> 		"Use of uninitialized value in string at ./script.pl line 29."

That's because if no argument is passed to the program then @ARGV is an 
empty list and $ARGV[0] is uninitialized.

Better is something like this:

   my $inst = $ARGV[0] or warn "No inst supplied";  # or die maybe

Putting quotes round your assignment isn't necessary; in fact it's 
causing your error message.

If you don't care about "consuming" your arguments you can code this:

   my $inst = shift or warn "whatever, or maybe die";

> 	- The array @dirs does not get the variable expansion for ${root} but $dir1 and $dir2 work fine.

Look up "Quote and Quote-like Operators" in the documentation; you'll 
find that the qw() operator doesn't interpolate variables, which is what 
you hoped it would do.

Some maven may be even now writing down a neat way for you to do this, 
but at the moment all I can think of is

  my @dirs= ("$root/path/to/directory","$root/path/to/directory2");

By the way, you don't need those braces round "root" in ${root} unless 
the parser would for some reason fail to see the word boundary.

   my $string = "$varone$vartwo $varthree $foo->{bar}"  # normal

If you wanted, say, to put a digit on the end of a variable to make 
another variable then you'd need braces:

  my $newvar = "$oldvar1";    # Crashes: $oldvar1 doesn't exist
  my $newvar = "${oldvar}1";  # Works.

-- 

Henry Law            Manchester, England


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

Date: Tue, 3 Mar 2015 11:44:23 -0800 (PST)
From: herbert.burnswell@gmail.com
Subject: Re: variable expansion issue
Message-Id: <d3ace14d-02c4-401e-b474-281b57ca249e@googlegroups.com>

Thanks all for your replies.  I must apologize for not being more clear.

It does appear what I was looking to do is not doable since qw() operator doesn't interpolate variables.  Here is a better picture of what I was looking for:

#!/usr/bin/perl

use strict;
use warnings;

my $inst        =       $ARGV[0];
my $root        =       "/my/path/to/$inst";
my $path1       =       "$root/path/today/1";
my $path2       =       "$root/path/yesterday/3";

my @dirs       =       qw("$root/path/needed1", "$root/path/needed2", "$root/path/needed3");

# ------------------------------------------------
sub usage
{

        print <<EOF;

        This script requires an argument, blah, blah..

EOF
#
}

# ------------------------------------------------

        if (!defined $ARGV[0]) {

        usage;
        exit 1;

}

# ------------------------------------------------

        chdir $path1 || die("$!");

                # Do something..

                print "$path1\n";

        chdir $path2 || die("$!");

                # Do something..

                print "$path2\n";

        foreach (@dirs){

                # Do something..

                chdir $_ || die("$!");

                print "$_\n";

        };

exit;


Do I simply have to use 3 blocks for each of the paths instead of using a foreach or is there a better way?

As you can see I am a perl novice..

TIA,

Herb


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

Date: Tue, 03 Mar 2015 22:52:25 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: variable expansion issue
Message-Id: <md56ua$196r$1@news.ntua.gr>

On 3/3/2015 9:44 μμ, herbert.burnswell@gmail.com wrote:
> Do I simply have to use 3 blocks for each of the paths instead of using a foreach or is there a better way?


#!/usr/bin/perl
use strict; use warnings; use feature qw/say/;

my $inst  = exists $ARGV[0] ? $ARGV[0] : die "script requires an arg\n";
my $root  = "/my/path/to/$inst";
my $path1 = "$root/path/today/1";
my $path2 = "$root/path/yesterday/3";
my $path3 = "/tmp";
my @dirs  = ( "$root/path/needed1", "$root/path/needed2", 
"$root/path/needed3");

foreach ($path1, $path2, $path3, @dirs)
{
say("dir \"$_\" is missing"), next unless -d $_;
chdir $_ || warn "Could not chdir to \"$_\" because \"$!\"\n";
say "successfule chdir at \"$_\"";
}



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

Date: Tue, 03 Mar 2015 23:38:34 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: variable expansion issue
Message-Id: <md59kr$1gsq$1@news.ntua.gr>

#!/usr/bin/perl
#!/usr/bin/perl
use strict; use warnings; use feature qw/say/;

my $inst  = exists $ARGV[0] ? $ARGV[0] : die "script requires an arg\n";
my $root  = "/my/path/to/$inst";
my $path1 = "$root/path/today/1";
my $path2 = "$root/path/yesterday/3";
my $path3 = "/tmp";
my @dirs  = ( "$root/path/needed1", "$root/path/needed2", 
"$root/path/needed3");


-d $_ ? chdir $_ ? say  "successfule chdir at \"$_\"" : warn "Could not 
chdir to \"$_\" because \"$!\"\n" : say  "dir \"$_\" is missing" for 
$path1, $path2, $path3, @dirs;





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

Date: Tue, 03 Mar 2015 23:41:43 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: variable expansion issue
Message-Id: <md59qo$1hbq$1@news.ntua.gr>

#!/usr/bin/perl
#!/usr/bin/perl
use strict; use warnings; use feature qw/say/;

my $inst  = exists $ARGV[0] ? $ARGV[0] : die "script requires an arg\n";
my $root  = "/my/path/to/$inst";
my $path1 = "$root/path/today/1";
my $path2 = "$root/path/yesterday/3";
my $path3 = "/tmp";
my @dirs  = ( "$root/path/needed1", "$root/path/needed2", 
"$root/path/needed3");




foreach my $dir ($path1, $path2, $path3, @dirs)
{
	if (-d $dir)
	{
		if (chdir $dir)
		{
		say  "successfule chdir at \"$dir\""
		}
		else
		{
		warn "Could not chdir to \"$dir\" because \"$!\"\n"
		}
	}
	else
	{
	say "dir \"$dir\" is missing"
	}
}



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

Date: Tue, 03 Mar 2015 21:51:24 +0000
From: Henry Law <news@lawshouse.org>
Subject: Re: variable expansion issue
Message-Id: <gaSdnc39251AsWvJnZ2dnUVZ8l6dnZ2d@giganews.com>

On 03/03/15 19:44, herbert.burnswell@gmail.com wrote:
> Do I simply have to use 3 blocks for each of the paths instead of using a foreach or is there a better way?

for my $thispath ( $path1, $path2 ) {
   print "This is '$thispath'\n";
   chdir( $thispath ) or die ...
}

But variables called "something", "something1", "something2", etc are a 
sure sign that restructuring is necessary.  Why not

   my @paths = ( "$root/path/today/1", "$root/path/yesterday/3" );
   for my $path ( @paths ) {
     chdir $path or die $!;
 ... etc

   or better might be
   my @relpaths = qw( path/today/1 path/yesterday/3 );
   for my $relpath ( @relpaths ) {
     chdir "$root/$relpath" or ...

I'm suspicious of all those literals in the path names (today/1 
yesterday/3 etc ...), unless they're just things you put in by way of 
example.  Care to tell us what you're trying to achieve overall?

-- 

Henry Law            Manchester, England


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

Date: Tue, 3 Mar 2015 14:31:27 -0800 (PST)
From: herbert.burnswell@gmail.com
Subject: Re: variable expansion issue
Message-Id: <df5fb66e-063e-45f1-af1b-b9b09ecf1073@googlegroups.com>

George and Henry, thank you very much for your replies.  I should be able to adjust my script given your provided examples.

Henry yes, the literal path names were strictly for examples.

thanks again..

Herb


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

Date: Wed, 4 Mar 2015 05:50:51 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: variable expansion issue
Message-Id: <md66fr$ppf$1@reader1.panix.com>

In article <d3ace14d-02c4-401e-b474-281b57ca249e@googlegroups.com>,
 <herbert.burnswell@gmail.com> wrote:
>It does appear what I was looking to do is not doable since qw()
>operator doesn't interpolate variables.

qqw(...) doesn't exist?!  I find that pretty shocking -- I expected
Perl to allow that obvious notion!

-- 
Tim McDaniel, tmcd@panix.com


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

Date: Wed, 4 Mar 2015 09:46:03 +0000 (UTC)
From: "Dave Saville" <No-Spam@deezee.org>
Subject: Re: variable expansion issue
Message-Id: <fV45K0OBJxbE-pn2-VmcElsohdE7F@paddington.bear.den>

On Tue, 3 Mar 2015 20:52:25 UTC, George Mpouras 
<gravitalsun@hotmail.foo> wrote:

> use strict; use warnings; use feature qw/say/;
> 

And the advantage of "say" over "print"?

TIA
-- 
Regards
Dave Saville


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

Date: Wed, 04 Mar 2015 10:36:47 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: variable expansion issue
Message-Id: <87egp5qcao.fsf@doppelsaurus.mobileactivedefense.com>

"Dave Saville" <No-Spam@deezee.org> writes:
> On Tue, 3 Mar 2015 20:52:25 UTC, George Mpouras 
> <gravitalsun@hotmail.foo> wrote:
>
>> use strict; use warnings; use feature qw/say/;
>
> And the advantage of "say" over "print"?

It appends a newline.


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

Date: Wed, 04 Mar 2015 10:46:44 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: variable expansion issue
Message-Id: <87a8ztqbu3.fsf@doppelsaurus.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> On 3/3/2015 9:44 μμ, herbert.burnswell@gmail.com wrote:
>> Do I simply have to use 3 blocks for each of the paths instead of using a foreach or is there a better way?
>
>
> #!/usr/bin/perl
> use strict; use warnings; use feature qw/say/;
>
> my $inst  = exists $ARGV[0] ? $ARGV[0] : die "script requires an arg\n";
> my $root  = "/my/path/to/$inst";
> my $path1 = "$root/path/today/1";
> my $path2 = "$root/path/yesterday/3";
> my $path3 = "/tmp";
> my @dirs  = ( "$root/path/needed1", "$root/path/needed2",
> "$root/path/needed3");
>
> foreach ($path1, $path2, $path3, @dirs)
> {
> say("dir \"$_\" is missing"), next unless -d $_;
> chdir $_ || warn "Could not chdir to \"$_\" because \"$!\"\n";

That's the same kind of problem we already had here not that long ago:
The filesystem (namespace) is a resource shared among all processes
running on the machine. This implies that the result of a stat-call
represent the state at the time the check was made and are not
necessarily still valid at any later time.


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

Date: Wed, 04 Mar 2015 11:10:05 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: variable expansion issue
Message-Id: <871tl5qar6.fsf@doppelsaurus.mobileactivedefense.com>

herbert.burnswell@gmail.com writes:
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $inst        =       $ARGV[0];
> my $root        =       "/my/path/to/$inst";
> my $path1       =       "$root/path/today/1";
> my $path2       =       "$root/path/yesterday/3";
>
> my @dirs       =       qw("$root/path/needed1", "$root/path/needed2", "$root/path/needed3");

A way to express this which works would be

my @dirs = map { "$root/$_" } qw(path/needed1 path/needed2 path/needed3);

>
> # ------------------------------------------------
> sub usage
> {
>
>         print <<EOF;
>
>         This script requires an argument, blah, blah..
>
> EOF
> #
> }
>
> # ------------------------------------------------
>
>         if (!defined $ARGV[0]) {
>
>         usage;
>         exit 1;
>
> }

JFTR: There's nothing wrong with doing the assignment before checking
that the value is defined.

> # ------------------------------------------------
>
>         chdir $path1 || die("$!");
>
>                 # Do something..
>
>                 print "$path1\n";
>
>         chdir $path2 || die("$!");
>
>                 # Do something..
>
>                 print "$path2\n";
>
>         foreach (@dirs){
>
>                 # Do something..
>
>                 chdir $_ || die("$!");
>
>                 print "$_\n";
>
>         };
>
> exit;
>
> Do I simply have to use 3 blocks for each of the paths instead of
> using a foreach or is there a better way?

This doesn't provide enough information to reply with anything
specific. Possibly useful idea:

----------
my @dirs = map { "/etc/$_" } qw(apache2 network default);

sub something
{
    print("I'm at ", `pwd`);
}

sub do_sth_at
{
    my ($dir, $action) = @_;

    chdir($dir) || die("chdir: $!");
    $action->();
    print("$dir\n");
}

do_sth_at($dirs[0], \&something);
do_sth_at($dirs[1], \&something);
do_sth_at($dirs[2], \&something);
-----------

Since the current working directory is a per-process property, it
usually makes sense to restore it after changing it in a subroutine.

------------
my @dirs = map { "/etc/$_" } qw(apache2 network default);

sub something
{
    print("I'm at ", `pwd`);
}

sub do_sth_at
{
    my ($dir, $action) = @_;
    my $old_wd;

    open($old_wd, '<', '.') or die("open: $!");

    chdir($dir) || die("chdir: $!");
    $action->();
    print("$dir\n");

    chdir($old_wd);
}

do_sth_at($dirs[0], \&something);
do_sth_at('.', \&something);
do_sth_at($dirs[1], \&something);
do_sth_at('.', \&something);
do_sth_at($dirs[2], \&something);
------------

Turning this into a demonstration why lexically scoped, automatic
resource cleanup is useful (despite a tracing garbage collector can't do
it);

------------
package OldWd;

sub new
{
    my $fh;

    open($fh, '<', '.') or die("open: $!");
    return bless(\$fh, $_[0]);
}

sub DESTROY
{
    chdir(${$_[0]});
}

package main;

my @dirs = map { "/etc/$_" } qw(apache2 network default);

sub something
{
    print("I'm at ", `pwd`);
}

sub do_sth_at
{
    my ($dir, $action) = @_;
    my $old_wd;

    $old_wd = OldWd->new();

    chdir($dir) || die("chdir: $!");
    $action->();
    print("$dir\n");
}

do_sth_at($dirs[0], \&something);
do_sth_at('.', \&something);
do_sth_at($dirs[1], \&something);
do_sth_at('.', \&something);
do_sth_at($dirs[2], \&something);


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

Date: Wed, 4 Mar 2015 10:50:22 +0000
From: Justin C <justin.1502@purestblue.com>
Subject: Re: variable expansion issue
Message-Id: <ek4jsb-pto.ln1@zem.masonsmusic.co.uk>

On 2015-03-04, Dave Saville <No-Spam@deezee.org> wrote:
> On Tue, 3 Mar 2015 20:52:25 UTC, George Mpouras 
> <gravitalsun@hotmail.foo> wrote:
>
>> use strict; use warnings; use feature qw/say/;
>> 
>
> And the advantage of "say" over "print"?

RSI reduction mechanism.


   Justin.

-- 
Justin C, by the sea.


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

Date: Wed, 04 Mar 2015 15:58:05 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: variable expansion issue
Message-Id: <md731g$153r$1@news.ntua.gr>

On 4/3/2015 12:46 μμ, Rainer Weikusat wrote:
> George Mpouras <gravitalsun@hotmail.foo> writes:
> That's the same kind of problem we already had here not that long ago:
> The filesystem (namespace) is a resource shared among all processes
> running on the machine. This implies that the result of a stat-call
> represent the state at the time the check was made and are not
> necessarily still valid at any later time.
>

this do not apply here.
This is unsafe

	stat $file;
	... if -f _;

This is safe

	stat $file;
	... if -f $file;

	


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

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 4383
***************************************


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