[12390] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5990 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jun 14 14:07:26 1999

Date: Mon, 14 Jun 99 11:01:39 -0700
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, 14 Jun 1999     Volume: 8 Number: 5990

Today's topics:
    Re: Extract file name from path <craig@mathworks.com>
        file rename script for WinNT (Mike Plemmons)
    Re: function to retrieve number of members in list (Hasanuddin Tamir)
        Insidious 'use constant' syntax bit me jboes@my-deja.com
    Re: Insidious 'use constant' syntax bit me <craig@mathworks.com>
        Mime...Perl...MS Outlook...and HTML <cpdog@rma.edu>
        my() in a loop <macintsh@cs.bu.edu>
    Re: my() in a loop <craig@mathworks.com>
    Re: my() in a loop <macintsh@cs.bu.edu>
    Re: Opening list of files in active perl jboes@my-deja.com
    Re: Opening list of files in active perl <hamptonk@bible.org>
    Re: Opening list of files in active perl <tchrist@mox.perl.com>
    Re: Opening list of files in active perl <tchrist@mox.perl.com>
    Re: Parellel operation sherifhanna@my-deja.com
    Re: pipe question <craig@mathworks.com>
    Re: simple perl question (Hasanuddin Tamir)
    Re: Sort log (Twarren10)
    Re: Sort log (Twarren10)
        Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)

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

Date: Mon, 14 Jun 1999 12:09:54 -0400
From: Craig Ciquera <craig@mathworks.com>
Subject: Re: Extract file name from path
Message-Id: <37652952.5106E1B1@mathworks.com>

Or:

use File::Basename;

$path = 'C:\dir1\dir2\dir3\file1.html';
$file   = basename( $path );

Craig

Faisal Nasim wrote:

> $path = 'C:\dir1\dir2\dir3\file1.html';
> $path =~ ?(.*)[\\/](.*)?;
> $file = $2;
>
> should work.
>
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> Faisal Nasim (the Whiz Kid)
> Web: http://wss.hypermart.net/
> AOL: Whiz Swift
> ICQ: 4265451
> FAX: (815) 846-2877
> Piyush Jain <pjain@usol.com> wrote in message
> news:375D5601.67D5F8AF@usol.com...
> >Hello everyone,
> >
> >I have a situation where the path could either be from the UNIX or
> >Windows machine of the form C:\dir1\dir2\dir3\file1.html
> >
> >I need to be able to read the file name file1.html into a variable. I am
> >new to perl so any help or suggestions would be appreciated.
> >
> >Thanks,
> >
> >piyush



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

Date: Mon, 14 Jun 1999 13:14:40 -0400
From: plemmons.1@osu.edu (Mike Plemmons)
Subject: file rename script for WinNT
Message-Id: <plemmons.1-1406991314400001@mac15.shs.ohio-state.edu>

Hello,
  I have recently downloaded ActiveState's newest perl set that combines
Win32 Perl and Active Perl.  I am trying to find a script that will allow
me to rename a file.  I have look at the help files that came with
ActiveState's perl and have found no help.  The example that they give for
rename is the same one from the The Perl Cookbook (which I own).

  As far as I can tell that same script does not work.  I have tried using
the dosglob examples they give and nothing seems to work.  I am beginning
to wonder that it is not possible.  I know the script works to the point
where I can see what file is being looked at.  It seems that the
rename($was,$_) is not working.

Here is what I have tried that has not worked.

rename.pl 's/-//g' *
and
rename.pl 's/-//g' *.*

Neither works right.  Any suggestions??

Thanks in advance,

rename.pl
-----------
$op = shift or die "Usage: rename expr [files]\n";
chop(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
}

-- 
Mike Plemmons | plemmons.1@osu.edu
WebMaster Speech and Hearing Science
http://www.shs.ohio-state.edu


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

Date: 15 Jun 1999 07:34:35 GMT
From: hasant@trabas.co.id (Hasanuddin Tamir)
Subject: Re: function to retrieve number of members in list
Message-Id: <slrn7mabm0.5r1.hasant@borg.intern.trabas.co.id>

On Sun, 13 Jun 1999 20:26:51 GMT, Bob Trieger <sowmaster@juicepigs.com> wrote:
> [ courtesy cc sent by mail if address not munged ]
>      
> dalehend@flash.net wrote:
> >
> >
> >Is there a function to return the number of members in a list or
> >array?
> 
> $#array will give you the last element in @array.
> 
> Since the first element is 0, you have to add 1 to this to get the 
> number of elements.

   @array = (0..5);
   $number_of_members = $#array + 1;    # 6
   $number_of_members = @array;         # 6
   $number_of_members = scalar(@array); # 6

scalar() is a builtin function.

   perldoc -f scalar.


HTH,

-- 
-hasan-
uhm, no more sig(h)


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

Date: Mon, 14 Jun 1999 15:48:29 GMT
From: jboes@my-deja.com
Subject: Insidious 'use constant' syntax bit me
Message-Id: <7k3889$ruk$1@nnrp1.deja.com>

Argh! I've been muttering to myself for weeks about how 'use constant'
didn't do what I wanted it to. I finally figured out that I'm a doofus,
because I read more into the perldoc entry than was actually there. I'm
posting this, in hopes it prevents some other apprentice Perl wizard
from falling into the same trap I did. And maybe a line or to will make
its way into the perldoc entry ...

I knew I could declare constants with

use constant PI => 3.1;   # Don't calc missle trajectories with this

but for some reason I thought you could declare multiple constants with

use constant
  A => 1,
  B => 2;   # This DOES NOT WORK or at least not the way you think...

Very odd behaviour results when you reference 'A', because it is a
*list* of 3 elements: 1, B, 2. 'B' as a constant is undefined.

This maddening bit of "Okay, if you really want to, go ahead and shoot
yourself in the foot" semantic behavior caused me untold grief for the
last couple of weeks as I "infected" several in-progress scripts with
this gee-whiz gadget and then couldn't figure out why the first
constant would sometimes give me what I wanted, and sometimes would
evaluate to '3' (number of elements in the list, duh!), while the
second constant would generally evaluate as undefined.

Sigh...

--
Jeff Boes  jboes@qtm.net
http://www.qtm.net/~jboes


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


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

Date: Mon, 14 Jun 1999 12:51:55 -0400
From: Craig Ciquera <craig@mathworks.com>
Subject: Re: Insidious 'use constant' syntax bit me
Message-Id: <3765332B.2D54585B@mathworks.com>

Just to add on here:

Don't forget that you can put constants into an array:

#!/usr/local/bin/perl -w

use strict;
use constant A => ( 1, 2, 3, 4 );

foreach my $constant ( A ) {
    print $constant . "\n";
}

This may be convenient in certain situations.

Craig

jboes@my-deja.com wrote:

> Argh! I've been muttering to myself for weeks about how 'use constant'
> didn't do what I wanted it to. I finally figured out that I'm a doofus,
> because I read more into the perldoc entry than was actually there. I'm
> posting this, in hopes it prevents some other apprentice Perl wizard
> from falling into the same trap I did. And maybe a line or to will make
> its way into the perldoc entry ...
>
> I knew I could declare constants with
>
> use constant PI => 3.1;   # Don't calc missle trajectories with this
>
> but for some reason I thought you could declare multiple constants with
>
> use constant
>   A => 1,
>   B => 2;   # This DOES NOT WORK or at least not the way you think...
>
> Very odd behaviour results when you reference 'A', because it is a
> *list* of 3 elements: 1, B, 2. 'B' as a constant is undefined.
>
> This maddening bit of "Okay, if you really want to, go ahead and shoot
> yourself in the foot" semantic behavior caused me untold grief for the
> last couple of weeks as I "infected" several in-progress scripts with
> this gee-whiz gadget and then couldn't figure out why the first
> constant would sometimes give me what I wanted, and sometimes would
> evaluate to '3' (number of elements in the list, duh!), while the
> second constant would generally evaluate as undefined.
>
> Sigh...
>
> --
> Jeff Boes  jboes@qtm.net
> http://www.qtm.net/~jboes
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.



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

Date: Mon, 14 Jun 1999 17:51:18 GMT
From: "Tim Clark" <cpdog@rma.edu>
Subject: Mime...Perl...MS Outlook...and HTML
Message-Id: <qib93.43$FF.8431@news.cwix.com>

This question is not specifically Perl related but I thought if anyone knew
the answer to this question it would be this newsgroup...

I have written a perl script to process a form etc. and have it sent me. I
am using MIME::LITE to send this form via email, because I want the email to
be output in HTML, which requires the appropriate MIME headers. Anyway, the
problem is that the email displays in HTML fine in Outlook Express but NOT
in microsoft outlook. The reason for this, I believe is that Outlook Express
looks for the mime type: text/html but Outlook (I think) will only read HTML
messages that another Outlook has sent, and it used a mime type called:
Application: Content-Type: application/ms-tnef
I have tried specifying this as the mime type in MIME::LITE but with no
success. When I try this approach Outlook just says "The Internet Mail
Connector received a message that could not be processed. View the original
content by opening the attached message."

So, the big question is: Does anyone know how to send a message to Outlook
that it will understand? Any help would be appreciated.
--Tim




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

Date: 14 Jun 1999 16:12:48 GMT
From: John Siracusa <macintsh@cs.bu.edu>
Subject: my() in a loop
Message-Id: <7k39m0$es$1@news1.bu.edu>

Consider these two code snippets:

Example 1:

    my($jedi, $color, $blades);

    foreach $jedi (@jedi)
    {
      ($color, $blades) = get_stats($jedi);
    }

Example 2:

    foreach my $jedi (@jedi)
    {
      my($color, $blades) = get_stats($jedi);
    }

For some reason, I have no qualms about doing "foreach my $var (...)",
but balk at putting a my() inside the loop (as in example 2), preferring
to declare those vars above the loop somewhere.  Benchmarking seems to
reveal that predeclaring all the my() vars before going through the loop
(as in the first example) is the fastest method, but the second example
seems more "popular" in ther Perl community.

So, where do you put your my()'s, and why?

-----------------+----------------------------------------
  John Siracusa  | If you only have a hammer, you tend to
 macintsh@bu.edu | see every problem as a nail. -- Maslow


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

Date: Mon, 14 Jun 1999 12:22:21 -0400
From: Craig Ciquera <craig@mathworks.com>
Subject: Re: my() in a loop
Message-Id: <37652C3D.3F4064F8@mathworks.com>

As a general rule of thumb I try and put MY my's as close to where I'm going
to use them as *possible ( this makes for more readable code, I think ).
Where in the vast majority of cases, I'll take readability over speed.

* Of course, how the variable is to be scoped plays a role in MY decision.

My $0.02

Craig

John Siracusa wrote:

> Consider these two code snippets:
>
> Example 1:
>
>     my($jedi, $color, $blades);
>
>     foreach $jedi (@jedi)
>     {
>       ($color, $blades) = get_stats($jedi);
>     }
>
> Example 2:
>
>     foreach my $jedi (@jedi)
>     {
>       my($color, $blades) = get_stats($jedi);
>     }
>
> For some reason, I have no qualms about doing "foreach my $var (...)",
> but balk at putting a my() inside the loop (as in example 2), preferring
> to declare those vars above the loop somewhere.  Benchmarking seems to
> reveal that predeclaring all the my() vars before going through the loop
> (as in the first example) is the fastest method, but the second example
> seems more "popular" in ther Perl community.
>
> So, where do you put your my()'s, and why?
>
> -----------------+----------------------------------------
>   John Siracusa  | If you only have a hammer, you tend to
>  macintsh@bu.edu | see every problem as a nail. -- Maslow



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

Date: 14 Jun 1999 16:32:23 GMT
From: John Siracusa <macintsh@cs.bu.edu>
Subject: Re: my() in a loop
Message-Id: <7k3aqn$es$2@news1.bu.edu>

Craig Ciquera <craig@mathworks.com> wrote:
> * Of course, how the variable is to be scoped plays a role in MY decision.

Oh, in case it wasn't clear, I'm talking about cases where you only
need to use the vars inside the loop.  That is, cases where example
2 would work just fine.  Obviously, if you need access to the vars
outside the loop, it's a non-issue.

-----------------+----------------------------------------
  John Siracusa  | If you only have a hammer, you tend to
 macintsh@bu.edu | see every problem as a nail. -- Maslow


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

Date: Mon, 14 Jun 1999 15:57:03 GMT
From: jboes@my-deja.com
Subject: Re: Opening list of files in active perl
Message-Id: <7k38o9$s6g$1@nnrp1.deja.com>

In article <3764F3FC.F6CAB946@bible.org>,
  Hampton Keathley <hamptonk@bible.org> wrote:
> Greetings,
>
> Sorry for the duplicate post. I realized I forgot something in my
> previous message.
>
> I have used PERL for Win32 for a long time and recently downloaded
> Active Perl.
>
> I often write scripts to clean up or convert massive numbers of files
in
> a directory. All my scripts that process several files at once don't
> work under
> active perl.
>
> First question is why?
>
> Most important question: what do I do to the following script to fix
it?
>
> I typically use the following:
> command line: perl script.pl *.txt
>
> ------------script----------------
> foreach $file (@ARGV){
>         ($fn,$ext) = split(/\./ ,$file);

<snip>

I think I see your problem... It's the OS, not Perl, that's at fault.

Your command line will be expanded into

perl script.pl file1.txt file2.txt file3.txt ...

but only under Unix and similar OS. Under DOS and related systems, the
'*.txt' parameter is not expanded before the script is invoked. You
would see a similar behaviour under Unix with

perl script.pl '*.txt'

Solution: instead of

> foreach $file (@ARGV){

use something like

foreach $file (glob($ARGV[0])){

The 'glob' function changes the parameter into a list of files. Note
that the 'globbing' is performed in the current directory, which means

perl script.pl c:/windows/*.txt

won't work.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


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

Date: Mon, 14 Jun 1999 17:18:18 GMT
From: Hampton Keathley <hamptonk@bible.org>
Subject: Re: Opening list of files in active perl
Message-Id: <3765390E.A0B47638@bible.org>

Thanks !! 

I Love those answers that don't just tell me why but also tell me how

foreach $file (glob($ARGV[0])){
instead of 
foreach $file (@ARGV){

works great!


Hampton


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

Date: 14 Jun 1999 11:43:00 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Opening list of files in active perl
Message-Id: <37653f24@cs.colorado.edu>

     [courtesy cc of this posting mailed to cited author]

In comp.lang.perl.misc, 
    Paul Bunkham <paul.bunkham@synetica.com> writes:
:> I typically use the following:
:> command line: perl script.pl *.txt

:I think that got answered with the fact that an emulation of the Unix
:wild card system is no longer in the Win32 Perl distributions.  I wonder
:why though, can someone give a response on that, as I thought the idea
:of porting a language is that it contains as many cross-platform
:features as possible, if it worked before, why isn't is still there?

Red herring.  Perl is not a command line shell.  The expansion of wild
cards before the execution of perl is something that must of course be
done before the execution of perl.  Real shells expand wild cards first
in order to provide a consistent interface to all programs.  Many people
use them.  I suggest that the original poster should as well.

--tom
-- 
	I get so tired of utilities with arbitrary, undocumented,
	compiled-in limits.  Don't you?


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

Date: 14 Jun 1999 11:49:33 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Opening list of files in active perl
Message-Id: <376540ad@cs.colorado.edu>

     [courtesy cc of this posting mailed to cited author]

In comp.lang.perl.misc, 
    Hampton Keathley <hamptonk@bible.org> writes:
:I Love those answers that don't just tell me why but also tell me how
:foreach $file (glob($ARGV[0])){

I'm afraid that that's wrong in the general case, which is why
one shouldn't do it!

What if you have a file named '***'?

--tom
-- 
The Unix Way of doing something [...] is to make it look as much like a filter
as possible.  (Richard O'Keefe)        


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

Date: Mon, 14 Jun 1999 15:58:24 GMT
From: sherifhanna@my-deja.com
Subject: Re: Parellel operation
Message-Id: <7k38qp$s7c$1@nnrp1.deja.com>



> You will probably want to use fork()
>
> /J\
> --
> "My codpiece layeth awkwardly across the nadgers" - Declan Donnelly

Thanks for the reply. This helps a lot.

Sherif :)


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


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

Date: Mon, 14 Jun 1999 12:02:25 -0400
From: Craig Ciquera <craig@mathworks.com>
Subject: Re: pipe question
Message-Id: <37652791.D6A8B232@mathworks.com>

Absolutely:

perldoc -f open

Craig

Faisal Nasim wrote:

> Is it possible that I call any application by pipe and some
> text to it (via pipe) and read its output to stdout in a variable?
>
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> Faisal Nasim (the Whiz Kid)
> Web: http://wss.hypermart.net/
> AOL: Whiz Swift
> ICQ: 4265451
> FAX: (815) 846-2877



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

Date: 15 Jun 1999 07:29:33 GMT
From: hasant@trabas.co.id (Hasanuddin Tamir)
Subject: Re: simple perl question
Message-Id: <slrn7maa24.5r1.hasant@borg.intern.trabas.co.id>

On Sun, 13 Jun 1999 18:59:13 GMT, 123yes@my-deja.com <123yes@my-deja.com> wrote:
> Hi all,
> 
> I am learning perl, can anyone teach me how can I write the following
> in perl?
> 
> 
> I wan to write if hour is between 0 and 12 then doing something, like :
> 
>                      if ($Hour > 0 < 12 ) {
>                      print "$morning";
>                      }
>                      elsif ($Hour > 12 < 18 ) {
>                      print "$afternoon";
>                      }
>                      elsif ($Hour > 12 < 24 ) {
>                      print "$evening";

You missed the last curly to close the last
elsif, well maybe it was just left out....

When I did

   perl -c file_name

it said:

   syntax error at ... line ..., near "0 <"

so I suppose some manpages could be a good reading. Type:

   perldoc perlsyn
   perldoc perlop

Or if you have ActivePerl installed, go to
perlsyn.html and perlop.html file.

You might have some certain reasons for quoting
those variables, but here again another good reading:

perlfaq4: What's wrong with always quoting ""$vars""?

Basically, you need something like this:

   if      ($Hour >  0 && $Hour < 12) {
      print $moring;
   } elsif ($Hour > 12 && $Hour < 18) {
      print $afternoon;
   } elsif ($Hour > 18 && $Hour < 24) {
      print $evening;
   }

Hmm, I think there's something missing here.
What to say if $Hour = 0, $Hour = 12, $Hour = 18,
and $Hour = 24?
Don't you want to print something differently? :-)

> but the server said i got some errors, can anyone point me to the right
> direction?

Actually, Perl already knows the errors. But since you
didn't ask, Perl told the server that some errors occured.
But the server didn't have any way to tell you what kind
of error.

One way to ask Perl to tell you if there might be some error
in your script, type:

   perl -cw script_name

like above.

> please email me.

I do.  Now you can read it in the same ng you posted to,
and you'll probably find some better answers from others.

HTH,

-- 
-hasan-
uhm, no more sig(h)


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

Date: 14 Jun 1999 16:41:34 GMT
From: twarren10@aol.com (Twarren10)
Subject: Re: Sort log
Message-Id: <19990614124134.22859.00001535@ng-fu1.aol.com>

>Because you now have references to anonymous arrays in you hash now rather
>than just your $score you have to ensure that you address this in your
>sort sub :

Man am I dense, or what! Thanks for your patience. You're right, got it working
now. Appreciate your help.
 


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

Date: 14 Jun 1999 16:43:09 GMT
From: twarren10@aol.com (Twarren10)
Subject: Re: Sort log
Message-Id: <19990614124309.22859.00001536@ng-fu1.aol.com>

>Hope this helps some more,

You guys were a great help. Thanks. Now I'm not going to pester you for a while
:) Appreciate your help.



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

Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>


Administrivia:

Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing. 

]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body.  Majordomo will then send you instructions on how to confirm your
]subscription.  This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.

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.  

To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.

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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 5990
**************************************

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