[31166] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2411 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 12 14:10:08 2009

Date: Tue, 12 May 2009 11:09:31 -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           Tue, 12 May 2009     Volume: 11 Number: 2411

Today's topics:
        Creating a hash with inferred key names? <mrstevegross@gmail.com>
    Re: Creating a hash with inferred key names? <uri@PerlOnCall.com>
    Re: Creating a hash with inferred key names? <mrstevegross@gmail.com>
    Re: Creating a hash with inferred key names? <jurgenex@hotmail.com>
    Re: Creating a hash with inferred key names? <willem@snail.stack.nl>
    Re: Creating a hash with inferred key names? <mrstevegross@gmail.com>
    Re: Creating a hash with inferred key names? <uri@PerlOnCall.com>
    Re: Creating a hash with inferred key names? <jurgenex@hotmail.com>
    Re: Creating a hash with inferred key names? <uri@PerlOnCall.com>
    Re: Creating a hash with inferred key names? <mrstevegross@gmail.com>
    Re: Creating a hash with inferred key names? <xueweizhong@gmail.com>
    Re: Creating a hash with inferred key names? <glex_no-spam@qwest-spam-no.invalid>
    Re: Creating a hash with inferred key names? <uri@PerlOnCall.com>
    Re: Creating a hash with inferred key names? <jurgenex@hotmail.com>
    Re: Creating a hash with inferred key names? <xueweizhong@gmail.com>
    Re: Creating a hash with inferred key names? <uri@StemSystems.com>
    Re: Creating a hash with inferred key names? <smallpond@juno.com>
    Re: Creating a hash with inferred key names? <smallpond@juno.com>
        Finding all the links in a Unix file/directory path <freesoft12@gmail.com>
    Re: Finding all the links in a Unix file/directory path <smallpond@juno.com>
    Re: Intel 17 Latest Intel Xeon processor <smallpond@juno.com>
        split string after third forwardslash <mikaelpetterson@hotmail.com>
    Re: split string after third forwardslash <josef.moellers@ts.fujitsu.com>
    Re: split string after third forwardslash <tadmc@seesig.invalid>
    Re: split string after third forwardslash <cartercc@gmail.com>
    Re: split string after third forwardslash <jurgenex@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 12 May 2009 08:07:50 -0700 (PDT)
From: mrstevegross <mrstevegross@gmail.com>
Subject: Creating a hash with inferred key names?
Message-Id: <12100b5d-02b8-4981-9f31-492cdc6556d0@l28g2000vba.googlegroups.com>

I've got a few variables that I'm using to create a hash: $foo, $bar,
and $baz. The keys for those values are the same as the variable
names: 'foo', 'bar', and 'baz'. Currently, I create the hash like so:

  my $hsh_ref = { 'foo' => $foo, 'bar' => $bar, 'baz' => $baz };

Given that the key names are identical to the variable names, is there
a way to create the hash so that I don't have to enter the key names
explicitly? Something like (in pseudocode):

  my $hsh_ref = { use_var_names_as_keys $foo, $bar, $baz };

Any ideas?

Thanks,
--Steve


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

Date: Tue, 12 May 2009 11:22:40 -0400
From: "Uri Guttman" <uri@PerlOnCall.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <87my9iz6pr.fsf@quad.sysarch.com>

>>>>> "m" == mrstevegross  <mrstevegross@gmail.com> writes:

  m> I've got a few variables that I'm using to create a hash: $foo, $bar,
  m> and $baz. The keys for those values are the same as the variable
  m> names: 'foo', 'bar', and 'baz'. Currently, I create the hash like so:

  m>   my $hsh_ref = { 'foo' => $foo, 'bar' => $bar, 'baz' => $baz };

  m> Given that the key names are identical to the variable names, is there
  m> a way to create the hash so that I don't have to enter the key names
  m> explicitly? Something like (in pseudocode):

  m>   my $hsh_ref = { use_var_names_as_keys $foo, $bar, $baz };

why did you keep those values in scalar vars to begin with? if you want
them in a hash now, what were you doing before with them that didn't
need a hash? there is no direct way to do what you want other than evil
stuff i won't even mention. so i suspect you have a poor design decision
from earlier which is pushing you into this corner. so backup, explain
the bigger problem and likely a much cleaner solution will show itself.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 12 May 2009 08:28:10 -0700 (PDT)
From: mrstevegross <mrstevegross@gmail.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <8305f14b-9249-49c0-9e12-07cf2ec8c603@n21g2000vba.googlegroups.com>

> why did you keep those values in scalar vars to begin with? if you want
> them in a hash now, what were you doing before with them that didn't
> need a hash? there is no direct way to do what you want other than evil
> stuff i won't even mention. so i suspect you have a poor design decision
> from earlier which is pushing you into this corner. so backup, explain
> the bigger problem and likely a much cleaner solution will show itself.

Wow, dude, that's a bit harsh. The reason why is that I need to first
verify that all of them are calculable before I put them in the hash.
The hash by definition has all the values defined, so I first need to
figure them out before I put them into the hash.

--Steve


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

Date: Tue, 12 May 2009 08:31:59 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <cf5j05pi2v2j06nptp8b79nii39qji1i4f@4ax.com>

mrstevegross <mrstevegross@gmail.com> wrote:
>I've got a few variables that I'm using to create a hash: $foo, $bar,
>and $baz. The keys for those values are the same as the variable
>names: 'foo', 'bar', and 'baz'. Currently, I create the hash like so:
>
>  my $hsh_ref = { 'foo' => $foo, 'bar' => $bar, 'baz' => $baz };
>
>Given that the key names are identical to the variable names, is there
>a way to create the hash so that I don't have to enter the key names
>explicitly? Something like (in pseudocode):
>
>  my $hsh_ref = { use_var_names_as_keys $foo, $bar, $baz };

Directly: no.
Opposite direction, i.e. given the names construct the variables, is
known as symbolic references and condemned here regularly (see "perldoc
-q variable name").

Indirectly: perldoc -f eval

jue


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

Date: Tue, 12 May 2009 15:37:51 +0000 (UTC)
From: Willem <willem@snail.stack.nl>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <slrnh0j5uf.3er.willem@snail.stack.nl>

mrstevegross wrote:
) Wow, dude, that's a bit harsh. The reason why is that I need to first
) verify that all of them are calculable before I put them in the hash.
) The hash by definition has all the values defined, so I first need to
) figure them out before I put them into the hash.

So use a second hash.  Or delete the non-calculable ones from the hash.
Or toss the whole calculable thing in a map operation.  Or ...


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


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

Date: Tue, 12 May 2009 08:48:36 -0700 (PDT)
From: mrstevegross <mrstevegross@gmail.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <2daf21a1-a050-4c96-8f06-676ec2d81544@l28g2000vba.googlegroups.com>

> Directly: no.
> Opposite direction, i.e. given the names construct the variables, is
> known as symbolic references and condemned here regularly (see "perldoc
> -q variable name").
>
> Indirectly: perldoc -f eval
>
> jue

Ok, good to know. I figured this was probably a long shot.

Thanks,
--Steve


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

Date: Tue, 12 May 2009 11:51:43 -0400
From: "Uri Guttman" <uri@PerlOnCall.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <874ovqz5dc.fsf@quad.sysarch.com>

>>>>> "m" == mrstevegross  <mrstevegross@gmail.com> writes:

  >> why did you keep those values in scalar vars to begin with? if you want
  >> them in a hash now, what were you doing before with them that didn't
  >> need a hash? there is no direct way to do what you want other than evil
  >> stuff i won't even mention. so i suspect you have a poor design decision
  >> from earlier which is pushing you into this corner. so backup, explain
  >> the bigger problem and likely a much cleaner solution will show itself.

  m> Wow, dude, that's a bit harsh. The reason why is that I need to first
  m> verify that all of them are calculable before I put them in the hash.
  m> The hash by definition has all the values defined, so I first need to
  m> figure them out before I put them into the hash.

it isn't as harsh as using those evil techniques which is what you are
asking for (even if you didn't know it). so i am smacking you to stop
you from burning your hand on the stove. get it?

you can calculate them and store them directly in the hash if you
want. there may be no reason for the scalar vars.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 12 May 2009 08:52:27 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <066j05pdv6tmreh1nh4qfl777j0njo1665@4ax.com>

mrstevegross <mrstevegross@gmail.com> wrote:
>> why did you keep those values in scalar vars to begin with? if you want
>> them in a hash now, what were you doing before with them that didn't
>> need a hash? there is no direct way to do what you want other than evil
>> stuff i won't even mention. so i suspect you have a poor design decision
>> from earlier which is pushing you into this corner. so backup, explain
>> the bigger problem and likely a much cleaner solution will show itself.
>
>Wow, dude, that's a bit harsh. The reason why is that I need to first
>verify that all of them are calculable before I put them in the hash.
>The hash by definition has all the values defined, so I first need to
>figure them out before I put them into the hash.

???
I thing there is some basic misunderstanding her. What do you mean by
"The hash [...] has all the values defined"? There is no problem
defining additional hash elements at any point in the program. Just do
	%myhash{newelem} = "whatever my heart desires";
to incrementally build up your hash.

I cannot imagine a situation where $foo and $hash{foo} would be any
different. There may be some odd situations like auto-vivication which
by nature of the issue is for hashes only, but otherwise they are
identical.

jue


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

Date: Tue, 12 May 2009 11:52:36 -0400
From: "Uri Guttman" <uri@PerlOnCall.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <87zldixqrf.fsf@quad.sysarch.com>

>>>>> "m" == mrstevegross  <mrstevegross@gmail.com> writes:

  >> Directly: no.
  >> Opposite direction, i.e. given the names construct the variables, is
  >> known as symbolic references and condemned here regularly (see "perldoc
  >> -q variable name").
  >> 
  >> Indirectly: perldoc -f eval
  >> 
  >> jue

  m> Ok, good to know. I figured this was probably a long shot.

and i said the same thing without naming the evil techniques for your
safety.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 12 May 2009 08:58:04 -0700 (PDT)
From: mrstevegross <mrstevegross@gmail.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <2579c422-265e-4128-9259-64b566afc39c@r34g2000vba.googlegroups.com>


> I thing there is some basic misunderstanding her. What do you mean by
> "The hash [...] has all the values defined"? There is no problem
> defining additional hash elements at any point in the program. Just do
> =A0 =A0 =A0 =A0 %myhash{newelem} =3D "whatever my heart desires";
> to incrementally build up your hash.
>
> I cannot imagine a situation where $foo and $hash{foo} would be any
> different. There may be some odd situations like auto-vivication which
> by nature of the issue is for hashes only, but otherwise they are
> identical.

By "defined", I meant "has valid values". That is, first I go through
a bunch of steps to figure out if I can indeed come up with valid
values for foo, bar, and baz. IFF all three can be calculated, then I
create the hash to store them. Else, the hash doesn't get defined at
all.

--Steve


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

Date: Tue, 12 May 2009 09:01:18 -0700 (PDT)
From: Todd <xueweizhong@gmail.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <540c3ffe-1130-43e8-b592-37127ec65098@y6g2000prf.googlegroups.com>

this may help:
>perl -MData::Dumper -e '$foo=1, $bar=2, $baz=3; $h{$_} = $$_ for qw/foo bar baz/; print Dumper \%h'
$VAR1 = {
          'bar' => 2,
          'baz' => 3,
          'foo' => 1
        };



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

Date: Tue, 12 May 2009 11:05:58 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <4a099e67$0$87077$815e3792@news.qwest.net>

mrstevegross wrote:
>> why did you keep those values in scalar vars to begin with? if you want
>> them in a hash now, what were you doing before with them that didn't
>> need a hash? there is no direct way to do what you want other than evil
>> stuff i won't even mention. so i suspect you have a poor design decision
>> from earlier which is pushing you into this corner. so backup, explain
>> the bigger problem and likely a much cleaner solution will show itself.
> 
> Wow, dude, that's a bit harsh. The reason why is that I need to first
> verify that all of them are calculable before I put them in the hash.
> The hash by definition has all the values defined, so I first need to
> figure them out before I put them into the hash.
> 
> --Steve

Possibly what Uri, who you neglected to cite in your reply, was
suggesting is that instead of setting $foo to something, set
$hash{ 'foo' } to something. Use a hash from the beginning,
instead of a scalar.

That way you could then use a hash slice, to create your $hsh_ref,
consisting of only acceptable keys.


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

Date: Tue, 12 May 2009 12:08:53 -0400
From: "Uri Guttman" <uri@PerlOnCall.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <87preexq0a.fsf@quad.sysarch.com>

>>>>> "T" == Todd  <xueweizhong@gmail.com> writes:

  T> this may help:

NO IT DOESN'T!

  >> perl -MData::Dumper -e '$foo=1, $bar=2, $baz=3; $h{$_} = $$_ for qw/foo bar baz/; print Dumper \%h'

we all said in this thread not to use symbolic refs as they are evil and
not wanted here. the solution lies with a different design.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 12 May 2009 09:18:16 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <q48j055vc9627c67cdk72fcbqco5epqq8v@4ax.com>

mrstevegross <mrstevegross@gmail.com> wrote:
>
>> I thing there is some basic misunderstanding her. What do you mean by
>> "The hash [...] has all the values defined"? There is no problem
>> defining additional hash elements at any point in the program. Just do
>>         %myhash{newelem} = "whatever my heart desires";
>> to incrementally build up your hash.
>>
>> I cannot imagine a situation where $foo and $hash{foo} would be any
>> different. There may be some odd situations like auto-vivication which
>> by nature of the issue is for hashes only, but otherwise they are
>> identical.
>
>By "defined", I meant "has valid values". That is, first I go through
>a bunch of steps to figure out if I can indeed come up with valid
>values for foo, bar, and baz. IFF all three can be calculated, then I
>create the hash to store them. Else, the hash doesn't get defined at
>all.

Well, what is blocking you from using $myhash{foo} instead of $foo (and
same for the others) to check if you can come up with valid values for
all of them?

jue


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

Date: Tue, 12 May 2009 09:36:44 -0700 (PDT)
From: Todd <xueweizhong@gmail.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <1b6312b1-9f2e-4c7b-ace9-80d5d8ea85b3@x31g2000prc.googlegroups.com>

> we all said in this thread not to use symbolic refs as they are evil and
> not wanted here. the solution lies with a different design.

Yes, they are evil. but Perl is attractive due to its evilness, more
natural way using map:

>perl -MData::Dumper -e '$foo=1, $bar=2; %h = map {$_ => $$_ } qw/foo bar baz/; print Dumper \%h'
$VAR1 = {
          'bar' => 2,
          'baz' => undef,
          'foo' => 1
        };
;)


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

Date: Tue, 12 May 2009 12:56:05 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <87eiuuxntm.fsf@quad.sysarch.com>

>>>>> "T" == Todd  <xueweizhong@gmail.com> writes:

  >> we all said in this thread not to use symbolic refs as they are evil and
  >> not wanted here. the solution lies with a different design.

  T> Yes, they are evil. but Perl is attractive due to its evilness, more
  T> natural way using map:

  >> perl -MData::Dumper -e '$foo=1, $bar=2; %h = map {$_ => $$_ }
  >> qw/foo bar baz/; print Dumper \%h'

map isn't the issue in your code. it is the symref that is evil and map
can't hide that. your code isn't strict clean and useless in the case
the OP asked about. if he has the keys already, why would he want the
vals in scalar vars first? either use a hash to begin with or do the
proper assignment when needed. symrefs have no benefits here. the rule
is use symrefs when you are munging the symbol table. don't use symrefs
for general data structure munging which is what your code does.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Tue, 12 May 2009 10:47:55 -0700 (PDT)
From: smallpond <smallpond@juno.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <eac2fd01-5230-4ff5-9ab4-37d1bf413e89@o27g2000vbd.googlegroups.com>

On May 12, 11:58 am, mrstevegross <mrstevegr...@gmail.com> wrote:
> > I thing there is some basic misunderstanding her. What do you mean by
> > "The hash [...] has all the values defined"? There is no problem
> > defining additional hash elements at any point in the program. Just do
> >         %myhash{newelem} = "whatever my heart desires";
> > to incrementally build up your hash.
>
> > I cannot imagine a situation where $foo and $hash{foo} would be any
> > different. There may be some odd situations like auto-vivication which
> > by nature of the issue is for hashes only, but otherwise they are
> > identical.
>
> By "defined", I meant "has valid values". That is, first I go through
> a bunch of steps to figure out if I can indeed come up with valid
> values for foo, bar, and baz. IFF all three can be calculated, then I
> create the hash to store them. Else, the hash doesn't get defined at
> all.

You say this as though there is a reason not to just start with the
hash.
Perhaps you worry that there is some cost or difficulty in defining
a hash vs 3 scalars.

Dont.



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

Date: Tue, 12 May 2009 10:55:09 -0700 (PDT)
From: smallpond <smallpond@juno.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <a413f07b-8449-44b8-bf28-3e51eb3865e2@b1g2000vbc.googlegroups.com>

On May 12, 12:56 pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> >>>>> "T" == Todd  <xueweizh...@gmail.com> writes:
>
>   >> we all said in this thread not to use symbolic refs as they are evil and
>   >> not wanted here. the solution lies with a different design.
>
>   T> Yes, they are evil. but Perl is attractive due to its evilness, more
>   T> natural way using map:
>
>   >> perl -MData::Dumper -e '$foo=1, $bar=2; %h = map {$_ => $$_ }
>   >> qw/foo bar baz/; print Dumper \%h'
>
> map isn't the issue in your code. it is the symref that is evil and map
> can't hide that. your code isn't strict clean and useless in the case
> the OP asked about. if he has the keys already, why would he want the
> vals in scalar vars first? either use a hash to begin with or do the
> proper assignment when needed. symrefs have no benefits here. the rule
> is use symrefs when you are munging the symbol table. don't use symrefs
> for general data structure munging which is what your code does.
>

Zen perl:

The perl beginner had a problem.
He said "Aha! I can solve this with symrefs."
Now he has two problems.


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

Date: Tue, 12 May 2009 07:23:26 -0700 (PDT)
From: "freesoft12@gmail.com" <freesoft12@gmail.com>
Subject: Finding all the links in a Unix file/directory path
Message-Id: <84905712-7ef7-4973-a4d8-55f0d0a66191@y10g2000prc.googlegroups.com>

Hi,

I am working on a Perl script that copies files from a log  file. Some
of the files are links. The link however is in an intermediate
directory. For example, in a path /tmp/test_hier/b/f/of3.cpp, the
actual path is /tmp/test_hier/b/c/of3.cpp.

I have attached a DirGen.pm that creates the sample directory
hierarchy and a DirGen.t that prints out the information that the file
is a link.


My Questions:
1) Does anyone know how I can recursively trace the links so that I
copy the "exact" hierarchy? For example, I want to copy *everything*
in /tmp/test_hier underneath /tmp/copy/tmp/test_hier (verbatim).
2) The complication is that there might be arbitrary number of links
between the linked path and the actual path. How can I trace all of
them?

All help is appreciated!
Regards
John


--- DirGen.pm

# this package creates a sample directory structure
package DirGen;

sub new {
    my $class = shift;
    my $work_dir = shift;
    my $self = {};
    $self->{working_dir_} = $work_dir;
    bless($self,$class);

    # create a sample hieararchy underneath $work_dir
    my @args = ("rm -rf $work_dir && mkdir -p $work_dir");
    system(@args) ==0 or die "system command failed:$?";

    # go to the working dir
    chdir "$work_dir" or  die "system command failed:$?";

    # the test directory tree
    # $work_dir/
    #           b/ - f -> c/     # f is a link to directory c/
    #             c/ - of3.cpp
    #
    my @dir_tree = ("mkdir -p b/c",
                    "touch b/c/of3.cpp",
                    "echo 4 > b/c/of3.cpp",
                    "cd b && ln -s c f");

    $self->generate_test_hier_(\@dir_tree);

    # access of3.cpp path via the directory link
    my $path = "$work_dir/b/f/of3.cpp";
    push(@{$self->{paths_}},$path);

    return $self;
}

# This function creates a predefined directory hierarchy
sub generate_test_hier_ {
    my $self = shift;
    my $dir_tree_cmds = shift;
    my @args = ();
    foreach my $path (@$dir_tree_cmds) {
      my @args = ($path);
      system(@args) ==0 or die "system command failed:$?";
    }
}

sub get_iterator {
    my $self = shift;
    my $pos = -1;
    return sub {
        return undef if ($pos > scalar(@{$self->{paths_}}));
        return  ${$self->{paths_}}[++$pos];
    }
}

1;
----- end of DirGen.pm

------ DirGen.t

eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}' && eval 'exec
perl -w -S $0 $argv:q'  if 0;
# This script tests DirGen.pm

require 5.006; # need perl version 5.6 or higher
use strict;      # comment out these two
use diagnostics; # and '-w' switch, once testing is over
use File::Basename;
use Cwd qw(realpath);

use DirGen;

my $dirTreeGen = DirGen->new("/tmp/test_hier");

my $dir_it = $dirTreeGen->get_iterator();
while (my $path = $dir_it->()) {
  my $target_dir = dirname($path);
  my $orig_dir = realpath($target_dir);# realpath works only with
directories
  if ($orig_dir ne $target_dir) {
    print "$path is a link, the actual path is $orig_dir/".basename
($path)."\n";
  }
}
-------- end of DirGen.t


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

Date: Tue, 12 May 2009 10:39:05 -0700 (PDT)
From: smallpond <smallpond@juno.com>
Subject: Re: Finding all the links in a Unix file/directory path
Message-Id: <ed71227c-066f-4260-a56d-dc3df07aeb24@b1g2000vbc.googlegroups.com>

On May 12, 10:23 am, "freesof...@gmail.com" <freesof...@gmail.com>
wrote:
> Hi,
>
> I am working on a Perl script that copies files from a log  file. Some
> of the files are links. The link however is in an intermediate
> directory. For example, in a path /tmp/test_hier/b/f/of3.cpp, the
> actual path is /tmp/test_hier/b/c/of3.cpp.
>
> I have attached a DirGen.pm that creates the sample directory
> hierarchy and a DirGen.t that prints out the information that the file
> is a link.
>

The test for a symbolic link in perl is: -l $name

>     my @dir_tree = ("mkdir -p b/c",
>                     "touch b/c/of3.cpp",
>                     "echo 4 > b/c/of3.cpp",
>                     "cd b && ln -s c f");
>

To make a directory in perl: mkdir $dirname;
To create a symbolic link in perl:  symlink OLDFILE,NEWFILE;



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

Date: Tue, 12 May 2009 08:30:03 -0700 (PDT)
From: smallpond <smallpond@juno.com>
Subject: Re: Intel 17 Latest Intel Xeon processor
Message-Id: <67aa6f3e-59c6-426a-bc89-6d048532190a@r34g2000vba.googlegroups.com>

On May 12, 7:01 am, whatnext <whatnex...@gmail.com> wrote:
> This is latest 17 Xeon processors are showing best conceptual future
> from another processor and this processor is very unique and providing
> best new technology.
>
> for more detailswww.infoaboutintelprocessor.blogspot.com


my $inheader = 1;
my $offtopic = 1;
my $mailto = 'groups-abuse@google.com';
my $ip = "unknown";

foreach (@lines) {
  /NNTP-Posting-Host: ([\d.]*)/ && $ip = $1;
  /perl/i && $offtopic = 0 unless $inheader;
  /^\n/ && $inheader = 0;
  /www.*blogspot.com/i && $src = $&;
}

if ($offtopic && $src) {
  $msg =  "To: $mailto\n";
  $msg .= "Bcc: $spamsaveaddress\n";  # A gmail account would be
appropriate!
  $msg .= "From: A. Phedup Yoozer\n";
  $msg .= "\n";
  $msg .= "Spam from $ip posted to comp.lang.perl.misc referencing:
$src\n\n";
  $msg .= join "",@lines;
  Email::Send->new({mailer => 'Sendmail'})->send($msg);
}

gmail and blogspot seem to be pits of spammers.
Seems like if someone spams a perl group, we should come up with a
perl solution.
Unfortunately, I don't have a direct NNTP feed.  Anybody?


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

Date: Tue, 12 May 2009 04:05:44 -0700 (PDT)
From: mike <mikaelpetterson@hotmail.com>
Subject: split string after third forwardslash
Message-Id: <ce1992c1-1ecb-42b0-954d-5eeb81543057@p4g2000vba.googlegroups.com>

Hi,

How can I use perl to get the following substring:

C:\cc_views\wong

from this string:

C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl

br,

//mike


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

Date: Tue, 12 May 2009 13:42:58 +0200
From: Josef Moellers <josef.moellers@ts.fujitsu.com>
Subject: Re: split string after third forwardslash
Message-Id: <gubnc3$jrt$2@nntp.fujitsu-siemens.com>

mike wrote:
> Hi,
> 
> How can I use perl to get the following substring:
> 
> C:\cc_views\wong
> 
> from this string:
> 
> C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl

What have you tried so far? Where did it fail to meet your expectations?

There are several ways to do so:
- use a regular expression (twice a path component followd by a 
backslash, then another path component)
- use split and then an array slice.

There are bound to be other methods,

Josef
-- 
These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html


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

Date: Tue, 12 May 2009 07:11:58 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: split string after third forwardslash
Message-Id: <slrnh0ipse.2tg.tadmc@tadmc30.sbcglobal.net>

mike <mikaelpetterson@hotmail.com> wrote:
> Hi,
>
> How can I use perl to get the following substring:
>
> C:\cc_views\wong
>
> from this string:
>
> C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl


If the full path is in $full, then use split() along with a "list slice":

    my $partial = join '\\', (split /\\/, $full)[0..2];


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Tue, 12 May 2009 07:51:55 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: split string after third forwardslash
Message-Id: <142d76c4-8919-4c10-a4bf-c1ec84a2101e@n8g2000vbb.googlegroups.com>

On May 12, 7:05=A0am, mike <mikaelpetter...@hotmail.com> wrote:
> Hi,
>
> How can I use perl to get the following substring:
>
> C:\cc_views\wong
>
> from this string:
>
> C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl

In most situations where you need to separate a string with values
delimited by some character(s), you can split the string into an array
variable by using split(), like:

$string =3D 'C:\cc_views\wong\mbv_admin\tools\scripts\deliver
\delivery.pl';
my @array =3D split(/\\/, $string);

This gives @array values like:
$array[0] =3D C:
$array[1] =3D cc_views
$array[2] =3D wong

And so on.

You can create a string using several methods, one of which is sprintf
(), like this:
$target =3D sprintf("%s\%s\%s",$array[0],$array[1],$array[2]);

This isn't the most concise way, but it's easy and understandable.
CC


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

Date: Tue, 12 May 2009 08:14:16 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: split string after third forwardslash
Message-Id: <f64j05ddrhh6mrbuhumjf5tdso1v0eb62o@4ax.com>

ccc31807 <cartercc@gmail.com> wrote:
>$target = sprintf("%s\%s\%s",$array[0],$array[1],$array[2]);

Ouch, this hurts. Are you programming Perl or C?

>This isn't the most concise way, but it's easy and understandable.

Well, why bother with sprintf() and its antics? IMO a good old plain
	$target = "$array[0]\\$array[1]\\$array[2]";
is way easier and more understandable.

Of course a 
	$target = join '\', @array[0..2];
is probably more perlish.

jue  	


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

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:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#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 2411
***************************************


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