[31167] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2412 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 12 21:09:46 2009

Date: Tue, 12 May 2009 18:09:11 -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: 2412

Today's topics:
    Re: Creating a hash with inferred key names? <uri@PerlOnCall.com>
    Re: Creating a hash with inferred key names? <darkon.tdo@gmail.com>
    Re: Finding all the links in a Unix file/directory path <freesoft12@gmail.com>
    Re: Finding all the links in a Unix file/directory path <ben@morrow.me.uk>
    Re: Finding all the links in a Unix file/directory path <jurgenex@hotmail.com>
    Re: Finding all the links in a Unix file/directory path <freesoft12@gmail.com>
    Re: Print strings based on regular expression <tzz@lifelogs.com>
        Simple date question <some@body.com>
    Re: Simple date question <veatchla@yahoo.com>
    Re: split string after third forwardslash <cartercc@gmail.com>
    Re: split string after third forwardslash <ben@morrow.me.uk>
    Re: split string after third forwardslash <cartercc@gmail.com>
    Re: split string after third forwardslash (Greg Bacon)
    Re: writing get_script as an external routine callable  <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

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

>>>>> "s" == smallpond  <smallpond@juno.com> writes:

  s> On May 12, 12:56 pm, "Uri Guttman" <u...@StemSystems.com> wrote:

  >> >> 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.

  s> Zen perl:

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

eggselent!! :)

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 17:14:24 -0400
From: "darkon" <darkon.tdo@gmail.com>
Subject: Re: Creating a hash with inferred key names?
Message-Id: <LfmdnXpTNs0ye5TXnZ2dnUVZ_uOdnZ2d@supernews.com>


"Uri Guttman" <uri@PerlOnCall.com> wrote in message 
news: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.

I guess he's not used to the straight-up tone of this group.  I remember you 
once nearly gagging at some of my code ("GACK!"), but I was used to clpm by 
then, and just laughed and paid attention to your advice.  (I think I used 
?: without assigning to anything as a short way of writing an if.)



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

Date: Tue, 12 May 2009 11:49:29 -0700 (PDT)
From: John <freesoft12@gmail.com>
Subject: Re: Finding all the links in a Unix file/directory path
Message-Id: <556a13d2-14e2-4c4d-a71a-f333285e5874@x1g2000prh.googlegroups.com>

Looks like my question was not clear:

In my logfile, i see the following path: /tmp/test_hier/b/f/of3.cpp.
Since directory 'f' is a symbolic link to directory 'c', the actual
path is /tmp/test_hier/b/c/of3.cpp.

I want to copy the orig path  /tmp/test_hier/b/f/of3.cpp to my target
directory: /tmp/copy/ . here is what I want to do:

1) Copy the actual file /tmp/test_hier/b/c/of3.cpp as /tmp/copy/tmp/
test_hier/b/c/of3.cpp
2) Create a link:  /tmp/copy/tmp/test_hier/b/f ->  /tmp/copy/tmp/
test_hier/b/c

Now /tmp/copy/tmp/test_hier/ is the same as /tmp/test_hier/. the
problems are:

1) 'realpath' just returns the actual file. It does not return how the
symbolic link is traced through the directories and the actual file is
found. I need that information.

2) there could an arbitrary number of links between the first symbolic
link file (/tmp/test_hier/b/f/of3.cpp) and the actual file i.e /tmp/
test_hier/b/f -> /tmp/test_hier/b/d -> /tmp/e -> /home/usr1/of3.cpp.

The problem: I have 2 paths in my hand: the symbolic link file ( /tmp/
test_hier/b/f/of3.cpp) and the actual file ( /home/usr1/of3.cpp).

Do any of you have any idea how to trace the intermediate symbolic
links?
Regards
John


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

Date: Tue, 12 May 2009 20:23:16 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Finding all the links in a Unix file/directory path
Message-Id: <4uuod6-52k.ln1@osiris.mauzo.dyndns.org>


Quoth John <freesoft12@gmail.com>:
> Looks like my question was not clear:
> 
> In my logfile, i see the following path: /tmp/test_hier/b/f/of3.cpp.
> Since directory 'f' is a symbolic link to directory 'c', the actual
> path is /tmp/test_hier/b/c/of3.cpp.
> 
> I want to copy the orig path  /tmp/test_hier/b/f/of3.cpp to my target
> directory: /tmp/copy/ . here is what I want to do:
> 
> 1) Copy the actual file /tmp/test_hier/b/c/of3.cpp as /tmp/copy/tmp/
> test_hier/b/c/of3.cpp
> 2) Create a link:  /tmp/copy/tmp/test_hier/b/f ->  /tmp/copy/tmp/
> test_hier/b/c
> 
> Now /tmp/copy/tmp/test_hier/ is the same as /tmp/test_hier/. the
> problems are:
> 
> 1) 'realpath' just returns the actual file. It does not return how the
> symbolic link is traced through the directories and the actual file is
> found. I need that information.
> 
> 2) there could an arbitrary number of links between the first symbolic
> link file (/tmp/test_hier/b/f/of3.cpp) and the actual file i.e /tmp/
> test_hier/b/f -> /tmp/test_hier/b/d -> /tmp/e -> /home/usr1/of3.cpp.
> 
> The problem: I have 2 paths in my hand: the symbolic link file ( /tmp/
> test_hier/b/f/of3.cpp) and the actual file ( /home/usr1/of3.cpp).
> 
> Do any of you have any idea how to trace the intermediate symbolic
> links?

readlink is the basic tool here. You will need to check the file and
then all enclosing directories up to the root, and resolve any links you
find yourself. Be careful to resolve relative links correctly. You will
then need to repeat the process on the resolves link.

Ben



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

Date: Tue, 12 May 2009 14:42:57 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Finding all the links in a Unix file/directory path
Message-Id: <h3rj05t3nbcusbvdf840jhdhmrjfnvpadr@4ax.com>

John <freesoft12@gmail.com> wrote:
>Looks like my question was not clear:
>
>In my logfile, i see the following path: /tmp/test_hier/b/f/of3.cpp.
>Since directory 'f' is a symbolic link to directory 'c', the actual
>path is /tmp/test_hier/b/c/of3.cpp.
>
>I want to copy the orig path  /tmp/test_hier/b/f/of3.cpp to my target
>directory: /tmp/copy/ . here is what I want to do:
>
>1) Copy the actual file /tmp/test_hier/b/c/of3.cpp as /tmp/copy/tmp/
>test_hier/b/c/of3.cpp
>2) Create a link:  /tmp/copy/tmp/test_hier/b/f ->  /tmp/copy/tmp/
>test_hier/b/c

So, if I understand correctly you want to copy the complete _structure_
of the the file tree, i.e. if a directory entry was a symlink in the
original then it should become a symlink in the copy, too.

I _think_ this is exactly the behaviour of File::Copy::Recursive. At
least the doc says

SymLinks
If your system supports symlinks then symlinks will be copied as
symlinks instead of as the target file. Perl's symlink() is used instead
of File::Copy's copy() You can customize this behavior by setting
$File::Copy::Recursive::CopyLink to a true or false value.

jue


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

Date: Tue, 12 May 2009 17:31:05 -0700 (PDT)
From: John <freesoft12@gmail.com>
Subject: Re: Finding all the links in a Unix file/directory path
Message-Id: <12aff533-27b0-4306-b963-e41dd883e981@d2g2000pra.googlegroups.com>

> So, if I understand correctly you want to copy the complete _structure_
> of the the file tree, i.e. if a directory entry was a symlink in the
> original then it should become a symlink in the copy, too.
>
> I _think_ this is exactly the behaviour of File::Copy::Recursive. At
> least the doc says
>
> SymLinks
> If your system supports symlinks then symlinks will be copied as
> symlinks instead of as the target file. Perl's symlink() is used instead
> of File::Copy's copy() You can customize this behavior by setting
> $File::Copy::Recursive::CopyLink to a true or false value.


I can't seem to use it:

---------- test.pl
my $orig_file = "/tmp/test_hier/b/f/of3.cpp";
my $target_dir = "/tmp/copy";

use File::Copy::Recursive qw(rcopy);
local $File::Copy::Recursive::CopyLink =1;

rcopy($orig_file,$target_dir)  or die $!;

----------

1) After executing the above script,  '/tmp/copy' is the file
'of3.cpp'.
2) I read the documentation and tried to see if setting
$File::Copy::Recursive::Maxdepth=1 made any difference but I am
getting the following warning:

Name "File::Copy::Recursive::Maxdepth" used only once: possible typo
at
        ./test.pl line 11 (#1)

Any ideas?

Regards
John


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

Date: Tue, 12 May 2009 14:11:14 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Print strings based on regular expression
Message-Id: <86skjajfvx.fsf@lifelogs.com>

On Mon, 11 May 2009 14:56:37 -0700 (PDT) Yong Huang <yong321@yahoo.com> wrote: 

YH> On Apr 20, 1:58 pm, Ted Zlatanov <t...@lifelogs.com> wrote:
>> On Sun, 19 Apr 2009 11:36:35 -0700 (PDT) Yong Huang <yong...@yahoo.com> wrote:
>> 
YH> Is there a program that prints all possible strings based on a regular
YH> expression?

>> See _Higher-Order Perl_ by Mark-Jason Dominus, chapter 6.5 (generating a
>> list of all the strings matched by a regular expression).  The whole
>> book is terrific.

YH> Thanks, Ted. After hours of testing the functions in that excellent
YH> book, I searched CPAN one more time and found

YH> http://search.cpan.org/~bowmanbs/Regexp-Genex-0.07/lib/Regexp/Genex.pm

YH> It's much easier to use. But currently it doesn't output all possible
YH> characters given character class or \w or \d (or I didn't get it to
YH> work). Every time it picks one but different character to output.
YH> Anyway, it's close to what I want.

Cool, I'm glad you found something you like, and I didn't know about
Regexp::Genex.

Ted


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

Date: Tue, 12 May 2009 19:49:55 -0400
From: somebody <some@body.com>
Subject: Simple date question
Message-Id: <pan.2009.05.12.23.49.55.609898@body.com>

Is there a simple way to get the date in the format mm/dd/yyyy into a
variable, without installing a module such as DateTime?


-Thanks



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

Date: Tue, 12 May 2009 19:05:20 -0500
From: l v <veatchla@yahoo.com>
Subject: Re: Simple date question
Message-Id: <DrednacEvMJck5fXnZ2dnUVZ_uKdnZ2d@supernews.com>

somebody wrote:
> Is there a simple way to get the date in the format mm/dd/yyyy into a
> variable, without installing a module such as DateTime?
> 
> 
> -Thanks
> 

I do it like:

my ($mon, $day, $year) = (localtime())[4,3,5];
$today = sprintf "%02d/%02d/%04d", $mon + 1, $day, $year +1900;
print $today;

output:
05/12/2009

-- 

Len


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

Date: Tue, 12 May 2009 12:50:30 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: split string after third forwardslash
Message-Id: <358fc5b7-ea40-467e-90f3-b71d9de40a2e@m24g2000vbp.googlegroups.com>

On May 12, 11:14=A0am, J=FCrgen Exner <jurge...@hotmail.com> wrote:
> Ouch, this hurts. Are you programming Perl or C?

Personally, I favor sprintf.

If you want to compare it to something that REALLY causes pain, look
at Lisp's format function:
(format t "~{~{~a:~10t~a~%~}~%~}" *db*))

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

or even $target =3D $array[0].'\'.$array[1].'\'.$array[2]

Does the solidus escape the following character with single quotes?

CC


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

Date: Tue, 12 May 2009 21:17:26 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: split string after third forwardslash
Message-Id: <m32pd6-qpl.ln1@osiris.mauzo.dyndns.org>


Quoth ccc31807 <cartercc@gmail.com>:
> On May 12, 11:14 am, Jürgen Exner <jurge...@hotmail.com> wrote:
> > Ouch, this hurts. Are you programming Perl or C?
> 
> Personally, I favor sprintf.
> 
> If you want to compare it to something that REALLY causes pain, look
> at Lisp's format function:
> (format t "~{~{~a:~10t~a~%~}~%~}" *db*))
> 
> > 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.
> 
> or even $target = $array[0].'\'.$array[1].'\'.$array[2]
> 
> Does the solidus escape the following character with single quotes?

What happened when you tried it?

(It escapes the delimiter or backslash itself. Other \X sequences are
passed through unchanged.)

Ben



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

Date: Tue, 12 May 2009 13:29:03 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: split string after third forwardslash
Message-Id: <6d01fc69-56b2-45d6-a74d-c813ba949891@r3g2000vbp.googlegroups.com>

On May 12, 4:17=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> What happened when you tried it?

I didn't try it, I just threw it out there. I'm just killing time
between a series of DB queries at work and one just popped so I hit
'Send.' Obviously, I'd find out what happened if I needed to write it,
but since I didn't I didn't.

> (It escapes the delimiter or backslash itself. Other \X sequences are
> passed through unchanged.)

Yeah, seems that I remember that now.

CC


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

Date: Tue, 12 May 2009 17:25:25 -0500
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: split string after third forwardslash
Message-Id: <cuSdnWPY-e7IapTXnZ2dnUVZ_vCdnZ2d@posted.hiwaay2>

mikaelpetterson@hotmail.com wrote

: 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 one of his presentations[*], mjd attributes the following rule
to Randal Schwartz: "Use capturing when you know what you want to
keep. Use split when you know what you want to throw away."

[*] http://perl.plover.com/flagbook/yak/Chi/slide066.html

You know what you want to keep, so

    $ cat try
    #! /usr/bin/perl

    use warnings;
    use strict;

    my $path = 'C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl';

    if ($path =~ /^(.+?\\.+?\\.+?)\\(.+)$/) {
      print "before: [$1]\n",
            "after:  [$2]\n";
    }
    else {
      print "No match.\n";
    }
    $ ./try
    before: [C:\cc_views\wong]
    after:  [mbv_admin\tools\scripts\deliver\delivery.pl]

Note the use of non-greedy +? to find the first three backslashes:
using greedy quanitifiers would give you the last three.

Hope this helps,
Greg
-- 
Computers in the future may have only 1,000 vacuum tubes and perhaps
only weigh 1.5 tons.
    -- Popular Mechanics, 1949


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

Date: Tue, 12 May 2009 19:36:37 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: writing get_script as an external routine callable by C
Message-Id: <l6sod6-17j.ln1@osiris.mauzo.dyndns.org>


Quoth frank@example.invalid:
> 
> So now I want main to have to call a routine to get the next $verse and
> $script.
> 
> @anything = get_script( $verse, \@s)
> 
> sub get_script
> {
> 
>   my ( $verse, @s ) = split;

Your sub (supposedly) takes a scalar and an array, but you are calling
it with a scalar and an arrayref. You need to decide which you want :).

What do you expect 'split' to do here? With no arguments it will split
$_ on whitespace, which means you will be using whatever value happens
to be in $_ at the time. I suspect you meant @_, which holds the
arguments passed to the sub.

>   my $script = join ' ', @s;
>   return something;
> }

    return something;

is not valid Perl. It will compile if you don't have 'use strict' turned
on, but it won't do what you expect. This is (part of) the reason why
you should always have 'use strict' turned on until you know better.

> Not exactly beautiful code, but my first efforts rarely look nice.  The
> reference for this in the camel book is §6 : Passing References.  p 224

If you actually want to pass a ref, your sub needs to look more like

    sub get_script {
        my ($verse, $sref) = @_;
        my $script = join " ", @$sref;

> Then there's the matter of calling a perl subroutine from C.

You haven't yet explained why you're trying to do this. Given your
inexperience with perl, it's almost certainly going to be easier to
stick with writing in one language at a time for now.

> #include <EXTERN.h>
> #include <perl.h>
> 
> static PerlInterpreter *my_perl;
> 
> int main(int argc, char **argv, char ** env) 
> {
>   char *args[] = {Null};
>   my_perl = perl_alloc();
>   perl_construct(my_perl);
>   perl_parse(my_perl, NULL, argc, argv, NULL);
>   call_argv("get_script", args);

Did you try to compile this? call_argv takes three arguments. Also, your
Perl sub is expecting at least two, and you haven't passed any Perl
arguments.

>   perl_destruct(my_perl);
>   perl_free(my_perl);
> 
>   return 0;
> }
> 
> The reference here is §21 of the camel book, p. 540.
> 
> Does any of this look close?

Reasonably so. You should be using perldoc perlembed as your reference
rather than the Camel book: the Camel was published shortly before the
release of perl 5.6.0, and a lot has changed since then. Most
importantly, you are missing PERL_SYS_INIT3 and PERL_SYS_TERM which must
be called first and last respectively.

You should also be aware that there are a number of undocumented
initializations that may or may not be necessary depending on your
platform. You can print a C file which ought to reproduce your current
perl binary by running

    perl -MExtUtils::Miniperl -ewritemain

Perl embedding is quite subtle, and I would not really recommend it
until you are familiar with writing XS. In order to do anything useful
with the embedded interpreter, you will need to know how to use the perl
API to get at and interpret the Perl values in the program.

You are passing main's argc/v to perl_parse, which means your program
will need to be called with the same command-line arguments as perl
would be. If you want your C program to run a particular Perl program,
you will need to create your own argv array to pass to perl_parse. Note
that you must still pass main's argc/v/env to PERL_SYS_INIT3.

If you really want to try this, you need to start by reading perlembed
and perlcall, and then probably perlapi and perlguts.

Ben



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

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


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