[24674] in Perl-Users-Digest
Perl-Users Digest, Issue: 6836 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 5 09:06:48 2004
Date: Thu, 5 Aug 2004 06:05:13 -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 Thu, 5 Aug 2004 Volume: 10 Number: 6836
Today's topics:
ANNOUNCE: File::MultiCat 0.03 <NOXwebmasterx@xmbstevensx.com>
ANNOUNCE: Config::Scoped <karl.gaissmaier@kiz.uni-ulm.de>
Re: any resources to assist with why perl is better? <matthew.garrish@sympatico.ca>
Re: Breakpoint on a builtin? <corff@cis.fu-berlin.de>
Re: Breakpoint on a builtin? (Anno Siegel)
CGI help (Steve)
Re: CGI help <mr@sandman.net>
Re: Confused by sorting array elements <mb@uq.net.au.invalid>
Re: Debugger angst <clint@0lsen.net>
Re: delimited data into nested array (Yup)
Re: delimited data into nested array <noreply@gunnar.cc>
Re: delimited data into nested array <go@away.spam.invalid>
Re: delimited data into nested array <mritty@gmail.com>
How can I get a nicer HTML output from POD parsers??? (dede)
Re: How do I detect if I have incoming data in <STDIN> <mr@sandman.net>
How do I detect if I have incoming data in <STDIN> when <andreas.berg@ericsson.com>
Re: How do I detect if I have incoming data in <STDIN> <andreas.berg@ericsson.com>
Re: if ( $a eq 1 || $b eq 2...) <matthew.garrish@sympatico.ca>
Re: if ( $a eq 1 || $b eq 2...) <matthew.garrish@sympatico.ca>
Re: if ( $a eq 1 || $b eq 2...) (Anno Siegel)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 3 Aug 2004 11:50:16 GMT
From: m <NOXwebmasterx@xmbstevensx.com>
Subject: ANNOUNCE: File::MultiCat 0.03
Message-Id: <I1y8H7.1DL4@zorch.sf-bay.org>
ANNOUNCE:
File::MultiCat (multicat_0.03.tar.gz) has been uploaded to CPAN.
http://search.cpan.org/~mbste/multicat_0.03/MultiCat.pm
NAME:
File::MultiCat
(Perl Module)
DESCRIPTION:
A concatinating website preprocessor.
Reads a description-file to make a website by concatinating files.
First filenames on each line are concatinated
to the last filename on that line, in order.
The description of how to build the pages for a website
can thus be written in minutes. This is useful in cases where
common menus, headers, and footers occur on each page.
It allows this to be done without the problems incurred
by frames.
This differs from most preprocessors in that it does not
use or interpret a macro language in a file being preprocessed;
instead it uses a site-description-file to build completed
pages from shorter files. Using this module, anyone can
now add this external ability to their macro oriented preprocessor with
a couple of lines of code.
The module is not limited to making pages of a website -- that
is just the most obvious use.
(( A similar package that is a script (as opposed to a module) is
available at:
http://www.mbstevens.com/preprocessor/index.html
The script in that package that corresponds to
'File::MultiCat' is 'mcat.pl'. The package also comes
with a macro based preprocessing script, 'mflt.pl'.
If you are not an author and just want to do some preprocessing
on your website, the script based version might be more handy. ))
CHANGELOG
0.01 Fri Jul 30 17:43:27 2004; original version.
0.02 Corrected several mistakes in stub documentation.
0.03 Added text of the Gnu Public License ("gpl.txt"), and made
a correction to README (Aug. 2, 2004)
USE
use File::MultiCat;
....
my $ob = File::MultiCat->new();
$ob->multicat('xot');
...with xot being the optional filename
of the site description file. It defaults to 'multicat.dat'.
REQUIRE
Perl 5.008 and standard modules strict and warnings.
Probably runs on older versions of Perl, but
hasn't been tested.
------------------------------
Date: Tue, 3 Aug 2004 11:35:07 GMT
From: Karl Gaissmaier <karl.gaissmaier@kiz.uni-ulm.de>
Subject: ANNOUNCE: Config::Scoped
Message-Id: <I1y8Hv.1qJF@zorch.sf-bay.org>
I am very pleased to announce a new submission to the CPAN,
Config::Scoped. Comments welcome.
From the README:
NAME
Config:Scoped - feature rich configuration file parser
FEATURES
Config::Scoped has the following highlights as a configuration file
parser:
* Complex recursive datastructures to any extent with scalars,
lists and hashes as elements,
* As a subset parses any complex Perl datastructures
(no references and globs) without *do* or *require*,
* Include files with recursion checks,
* Controlled macro expansion in double quoted tokens,
* Lexically scoped parameter assignments and pragma directives,
* Perl quote like constructs to any extent, '', "", and
here docs <<,
* Perl code evaluation in Safe compartments,
* Caching and restore with MD5 checks to determine
alterations in the original config files,
* Standard macro, parameter, declaration redefinition
validation, may be overridden to validate on semantic
knowledge,
* Standard file permission and ownership safety validation,
may be overridden,
* Fine control for redefiniton warnings with pragma's
and other safety checks,
* Easy inheritable, may be subclassed to build parsers
with specialized validation features,
* Condoning syntax checker, semicolons and or commas are not always
necessary to finish a statement or a list item if the end can be
guessed by other means like newlines, closing brackets,
braces etc.,
* Well spotted messages for syntax errors even within include
files with correct line numbers and file names,
* Exception based error handling,
* etc.,
PREREQUISITES
Parse::RecDescent, Error
Standard modules:
Carp
Storable
File::Spec
File::Basename
Digest::MD5
Safe
APPETIZER
Configuration file example:
# default parameters
community = public;
variables = [ ifInOctets, ifOutOctets ];
oids = {
ifInOctets = 1.3.6.1.2.1.2.2.1.10;
ifOutOctets = 1.3.6.1.2.1.2.2.1.16;
};
%warnings parameter off; ### allow parameter redefinition
# declarations
devices rtr001 {
ports = [ 1, 2, 8, 9 ];
}
devices rtr007 {
community = 'really top secret!';
ports = [ 1, 2, 3, 4 ];
}
INSTALLATION
It's all pure Perl.
perl Makefile.PL
make
make test
make install
AUTHOR
Karl Gaissmaier (karl.gaissmaier@kiz.uni-ulm.de)
COPYRIGHT
Copyright (c) 2004, Karl Gaissmaier. All Rights Reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
--
Karl Gaissmaier KIZ/Infrastructure, University of Ulm, Germany
Email:karl.gaissmaier@kiz.uni-ulm.de Service Group Network
Tel.: ++49 731 50-22499
------------------------------
Date: Wed, 4 Aug 2004 20:59:41 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: any resources to assist with why perl is better?
Message-Id: <1EfQc.28910$Jq2.1245279@news20.bellglobal.com>
"davout" <davZZZYYYXXXout@dial.pipex.com> wrote in message
news:41111c01$0$26982$cc9e4d1f@news.dial.pipex.com...
> Can anybody help point me at some web resources that explain why Perl is
> better than say Python, TCL etc?
>
They're all useless languages, unless they can do something useful for you.
Identify your needs and then find a language that meets them. Most of the
generalizations have some grain of truth to them (i.e., Perl for sysadmin
and text processing, the Cs for writing apps, vb for "dim"-witted Windows
programming... ; )
Matt
------------------------------
Date: 5 Aug 2004 07:31:47 GMT
From: <corff@cis.fu-berlin.de>
Subject: Re: Breakpoint on a builtin?
Message-Id: <2ne633Fv21e9U1@uni-berlin.de>
Keith NoSpam Arner <spam.keith.me.arner.not@marconi.com> wrote:
: Do the builtin functions reside in a special namespace that will allow me
: to, e.g., 'b special::unlink', or some such? Or is there some other,
: special notation that I can use?
iirc Perl doesn't have unlink and related functions itself but uses
system calls for those; in this case it is understandable that there
is no stepping into this part of code.
I suggest you run your script under strace, like
$ strace -o strace.dump myperl-script-with-bizarre-unlink-in-it.pl
and analyze the log in strace.dump which is definitely informative.
Oliver.
--
Dr. Oliver Corff e-mail: corff@zedat.fu-berlin.de
------------------------------
Date: 5 Aug 2004 08:05:24 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Breakpoint on a builtin?
Message-Id: <cespo4$5un$1@mamenchi.zrz.TU-Berlin.DE>
Keith NoSpam Arner <spam.keith.me.arner.not@marconi.com> wrote in comp.lang.perl.misc:
>
> Is there a way in the perl debugger to set a breakpoint on a builtin
> function?
>
> I currently have a script that is deleting a file, and I want to know why
> it has decided to delete the file. I'd like to set a breakpoint on the
> 'unlink' function, so I can catch the caller, and do the whole debugging
> thing. However, when I try this, I get:
>
> DB<1> b unlink
> Subroutine main::unlink not found.
>
> Do the builtin functions reside in a special namespace that will allow me
> to, e.g., 'b special::unlink', or some such? Or is there some other,
> special notation that I can use?
You could override (documented in perlsub) the unlink function and set
a breakpoint on that. This should do (untested, I'm stalwartly
ignoring the debugger):
# double braces indicate an un-indented block
{{
package CORE::GLOBAL;
use subs 'unlink';
sub unlink { CORE::unlink( @_) }
}}
unlink 'xyz';
Anno
------------------------------
Date: 4 Aug 2004 20:37:56 -0700
From: steve@foutty.org (Steve)
Subject: CGI help
Message-Id: <38f8b5b0.0408041937.276a3a62@posting.google.com>
I need to display a neighbor state from my Cisco routers on our
internal website. What would be the easiest way to do this..
Thanks,
Steve
------------------------------
Date: Thu, 05 Aug 2004 12:45:26 +0200
From: Sandman <mr@sandman.net>
Subject: Re: CGI help
Message-Id: <mr-E803CD.12452605082004@individual.net>
In article <38f8b5b0.0408041937.276a3a62@posting.google.com>,
steve@foutty.org (Steve) wrote:
> I need to display a neighbor state from my Cisco routers on our
> internal website. What would be the easiest way to do this..
This isn't a very good question, since you seem to be assuming that there are
people here that has done this before and you want them to share how they did
it. While you might get lucky, you'll probably get more answers if you tell us
what you want to do more specifically.
Perl is mostly used to automate things you would normally do by hand, which is
exactly what you want to do here. But in order for us to give you advice on how
to automate the otherwise manual process, you need to detail the manual process.
So, how do you go about to find out the "neighbor state" of a cisco router? Do
you telnet to it? What command do you use when you have telnetted to it? Does
it require a login name and a password? Do want this to happen on an interval
or each time someone visits the web page that is supposed to contain the
information.
For example, if I wanted to display the output of the command "uptime" on my
Mac on my website, which is running Linux, I would probably write a script that
looks something like this:
#!/usr/bin/perl
use strict;
use warnings;
print qx{ssh sandman\@<hostname> uptime};
__END__
Out: 12:32pm up 274 days, 14:08, 0 users, load average: 0.84, 0.93, 0.83
In this particular case, I am using SSH, and authorized keys needs to be used,
so I don't need to supply a password. I believe there is a Net::Telnet CPAN
module for Telnet, which one could use (I don't use telnet on any host, so I
wouldn't know).
Anyway, some advice on how to ask questions and some advice on how to fetch
data from a remote host which might give you a nudge. If not, be sure to ask
your question again, being a little more specific.
--
Sandman[.net]
------------------------------
Date: Thu, 05 Aug 2004 17:16:38 +1000
From: Matthew Braid <mb@uq.net.au.invalid>
Subject: Re: Confused by sorting array elements
Message-Id: <cesmsm$5jq$1@bunyip.cc.uq.edu.au>
Perl User wrote:
> I am thinking that the best way to do this is to go through
> the database first and pull out all the records for the year.
> With each pull I would combine maidenname and lastname into
> one variable. Then sort based on the new variable.
Thats a kind of sucky way to sort it.
Assuming that @lines contains each line as an array reference with each field as
an element (element 3 being the maiden name, element 4 being the last name):
# Untested - Schwartzian Transform used because of the RE - might be too
# slow otherwise.
@lines = map {$_->[0]}
sort {$a->[1] cmp $b->[1]}
map {[$_, $_->[3] =~ /^\s*$/ ? $_->[4] : $_->[3]]}
@lines;
This handles a maiden name of any string of white space being ignored and the
lastname being used instead. If you know that the maiden name will be an empty
string if its blank (instead of just any white-space string) you can do:
# Untested - probably fast enough to not require the S.T. from before.
# Note the horrible line length - means a funny line break, but meh.
@lines = sort {(length($a->[3]) ? $a->[3] : $a->[4]) cmp
(length($b->[3]) ? $b->[3] : $b->[4])} @lines;
I leave it up to you to find out what a Schwartzian Transform is, and whether
I've spelled it correctly :)
MB
------------------------------
Date: Thu, 05 Aug 2004 01:07:15 GMT
From: Clint Olsen <clint@0lsen.net>
Subject: Re: Debugger angst
Message-Id: <slrnch3222.neh.clint@belle.0lsen.net>
On 2004-07-30, Peter Scott <peter@PSDT.com> wrote:
>
> Which is why I developed the 'y' command which allows you to inspect
> lexicals at higher stack frames. perldoc perldebug.
Ok, I managed to get this installed, and here are my thoughts on this:
1) Since 'T' doesn't print out the numbers of the stack frames, I end up
using 'wc -l' to calculate my 'uplevel'. This is really annoying.
2) This command (and apparently 'X' and 'Y') don't take qualified type
names or references to such, just the literal text name. Why? So, if I
wanted to inspect a multi-level data structure (array, hash, or
whatever), I either get all or none of it. In our case, that was about
1/4 mi. of text :)
3) 'y' and friends don't have a depth limit so you don't get the reams of
data mentioned in #2.
On another completely independent gripe:
Why doesn't the perl debugger take File:line input as debug points? Every
time I have to type "f Package.pm" and "b lineno", it's very aggravating.
Don't get my wrong, 'y' is a big improvement over what I had before -
zip/nada for peeking at the stack. I just wanted to point out some usage
model issues that could use some refinement.
It would be interesting if you could interject on the effort involved on
any of these points. I'd even consider trying to add them with my fumble
fingers if it was feasible. I either need to do that or get the heck out
of using Perl. :)
-Clint
------------------------------
Date: 5 Aug 2004 00:43:07 -0700
From: yaweh32@yahoo.com (Yup)
Subject: Re: delimited data into nested array
Message-Id: <c082ca84.0408042343.f7cff21@posting.google.com>
>while (<INFO>) {
> chomp;
> push @A, [ split /\t/ ];
Many thanks everyone for your help. The above code was actually what I
had been searching for, but I kept being told that Perl didn't
directly support multi-dimensional arrays. If this is the case, what
is this called, then?
But now that I can access @A in the same way as a C++ vector, it suits
me well.
------------------------------
Date: Thu, 05 Aug 2004 10:37:54 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: delimited data into nested array
Message-Id: <2nea94FvdskaU1@uni-berlin.de>
Yup wrote:
>> while (<INFO>) {
>> chomp;
>> push @A, [ split /\t/ ];
>
> Many thanks everyone for your help. The above code was actually
> what I had been searching for, but I kept being told that Perl
> didn't directly support multi-dimensional arrays.
Didn't you look in the Perl docs?
http://www.perldoc.com/perl5.8.4/pod/perldsc.html
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Thu, 05 Aug 2004 08:00:42 GMT
From: LaDainian Tomlinson <go@away.spam.invalid>
Subject: Re: delimited data into nested array
Message-Id: <vff7u1-o9q.ln1@news.bclennox.com>
On 2004-08-04, 'Yup' <yaweh32@yahoo.com> wrote in comp.lang.perl.misc:
> Hello,
> I'm just starting to learn Perl. Up until now I'd been struggling to
> learn how to import a tab-delimited data table (from a text file) into
> Perl as a two dimensional nested array. I wanted to be able to
> manipulate that data by accessing it using x-y coordinates, and then
> output it again.
>
> I may have been looking in the wrong place, but I had a hard time
> finding help on Google groups and other places online. However, I've
> managed to figure it out, and thought I'd post my code.
>
> For two reasons, I suppose - to help other novices, and perhaps get
> some comments from more advanced users. On the latter part, I'm
> interested in knowing what I could do to make my code run faster and
> make it more Perl-ish.
There is lots to correct, I'm afraid. No offense, but I hope that the
other novices will continue reading the thread rather than stopping at
this solution (or mine, for that matter).
> #!/usr/local/bin/perl -w
Good, but not good enough. Get rid of the -w and add:
use warnings;
use strict;
Then you'll have to put a 'my' in front of all the variables you
declare. It'll break some other things too, but I promise it's for the
best.
> #define filename and open it
> $file = 'a1.txt';
> open(INFO, $file) || die "Can't open file $file\n" ;
Let perl tell you why it died by including $! in the output:
open my $INFO, '<', $file or die "Couldn't open $file: $!";
> # read file into temporary array called "lines"
> while(<INFO>)
> {
> #chop off the carrage return
> chop $_;
> push @lines, $_;
> }
Bleh. Do you have a good reason for reading the file into an array
rather than performing the data manipulation and immediately writing to
the file? If you're reading really huge files into arrays, you're going
to run into memory problems. Use the following construct if you can:
my $outfile = 'some_other.txt';
open my $OUTFILE, '<', $outfile or die "Couldn't open $outfile: $!";
# output line terminator
$\ = "\n";
while ( <$INFO> ){
chomp; # chomp is safer than chop
my @fields = split /\t/;
my @new_fields = do_stuff_with( @fields );
print $OUTFILE join "\t" => @new_fields;
}
If you'd still like to use the arrays, read up on perllol and use this:
my @AoA; # an array of arrays
while ( <$INFO> ){
chomp;
my @fields = split /\t/;
# push references to arrays, not arrays themselves
push @AoA => \@fields;
}
my @new_AoA = do_stuff_with( @AoA );
# open, etc.
print $OUTFILE join "\t" => @$_ for @new_AoA;
That will basically do everything you wanted, but I'll put some comments
below as well.
> # Close the file
> close(INFO);
If you use lexical filehandles like above, you don't need to close them
explicitly. They are closed when they go out of scope (in this case,
when you exit the program; in others, when you exit a subroutine or
other block).
> #reset index for generating arrays named "line_{$i}"
> $i = 0;
Please do not use variables in this way. You're already familiar with
arrays, and you've probably seen hashes (associative arrays) as well.
Use them whenever you're tempted to try this. The 'use strict' above
will prevent this mistake. See:
perldoc -q "How can I use a variable as a variable name?"
> #read each array entry into new array, split with tab
> foreach (@lines)
> {
> push @{'line_'.${i}}, split("\t", $lines[$i]);
> $i++
> }
>
> #generate an array to hold the other arrays
> for ($i=0; $i<scalar(@lines); $i++)
> {
> push @A, *{'line_'.${i}};
> }
You won't very often need for loops like this in Perl. In this case,
you could use the range operator if you wanted. It's usually easier
just to loop over the list itself:
# range operator
for ( 0..$#lines ){ ... }
# aliasing
for my $line ( @lines ){ ... }
> #rename the top corner to be START
> $A[0][0] = "START";
>
> #open file to send data to
> open(OUTFILE, ">a1_edited.txt");
Always check the return value of open. Always. Yes, always.
open( ... ) or die "Oh well: $!";
> for ($i=0; $i<scalar(@lines);$i++)
> {
>
> for ($j=0;$j<scalar(@line_0);$j++)
> {
> print OUTFILE "$A[$i][$j]";
> if ($j<(scalar(@line_0)-1)) { print OUTFILE "\t";}
> }
>
> print OUTFILE "\n";
> }
> close(OUTFILE);
This whole loop was condensed above by using join(), $\, and that handy
array of arrays (array references, really).
Hopefully this will be useful to you. Perl is pretty complicated and
features a lot of little tricks to make your code cleaner. It takes a
long time to learn them all (I'm approximately 3% of the way there).
Good luck,
Brandan L.
--
bclennox \at eos \dot ncsu \dot edu
------------------------------
Date: Thu, 5 Aug 2004 07:24:56 -0400
From: Paul Lalli <mritty@gmail.com>
Subject: Re: delimited data into nested array
Message-Id: <20040805072030.O4433@barbara.cs.rpi.edu>
On Thu, 5 Aug 2004, Yup wrote:
> >while (<INFO>) {
> > chomp;
> > push @A, [ split /\t/ ];
>
> Many thanks everyone for your help. The above code was actually what I
> had been searching for, but I kept being told that Perl didn't
> directly support multi-dimensional arrays. If this is the case, what
> is this called, then?
It's an array of array references. Arrays in Perl can only contain
scalars. A reference is a scalar. To mimic the functionality of
multi-dimensional arrays (or hashes for that matter), you populate an
array with references to other arrays. In this case, the [ ] create an
anonymous array reference (ie, a reference to an array that doesn't have a
'name', like @words). That anonymous array reference is then pushed into
the array @A.
> But now that I can access @A in the same way as a C++ vector, it suits
> me well.
Be careful. While you can pretend that a Perl array only has the
functionality of a vector, they're not the same data structures. A vector
only allows insertions and removals from the 'back'. A Perl array can be
modified at the back (using push and pop), the front (using shift and
unshift), or arbitrarily in the middle (using splice and array index []
notation).
Paul Lalli
------------------------------
Date: 5 Aug 2004 05:33:00 -0700
From: abrey@gmx.net (dede)
Subject: How can I get a nicer HTML output from POD parsers???
Message-Id: <166f59a6.0408050433.5f01a94f@posting.google.com>
Dear community,
from using the tools pod2html and pod2htmltree I understand that I might add
a stylesheet-file (*.css) to modify the appearance of the generated tags of
the html-resultfiles.
Now, what I would like to understand is how to use e.g.
- these fancy grey tables for code fragments (usually parsed with <pre>-tag) !
- colored boxes around the head1, head2 tags (like activestate) !
- special: embedd pictures (ie. GIF or JPG-links) !
Can anybody tell me what tool, module, configuration I have to use in order
to modify the output of the POD -> HTML parsing process?
Thank you in advance,
Andreas
------------------------------
Date: Thu, 05 Aug 2004 14:13:01 +0200
From: Sandman <mr@sandman.net>
Subject: Re: How do I detect if I have incoming data in <STDIN> when I pipe something to my perl script ?
Message-Id: <mr-0E2FB8.14130105082004@individual.net>
In article <cet6gg$5g5$1@newstree.wise.edt.ericsson.se>,
Andreas Berg <andreas.berg@ericsson.com> wrote:
> Greetings,
>
> I'm writing a script that is supposed to fetch information from <STDIN>
> that is piped to the script, something like this:
> > cat file.txt | myscript.pl
>
> With this, I will get what I want into <STDIN> and I can read it. But,
> this is the only case I want to read it. I do not want my script to try
> to read from a file in case I do have any command line paramters and I
> do not want the script to wait for keyboard input. If something is piped
> to the script, I want that data, if nothing is piped to the script, I
> dont want it to do anything with <STDIN>, how do I accomplish this ?
>
> regards,
> Andreas
cat file.txt | myscript.pl -stdin
And have myscript.pl read from <STDIN> only if $ARGV[0] is "-stdin", or use
getopts if you want more elaborate control of the parameters. This is, if I am
reading this line correctly:
"I do not want my script to try to read from a file in case I do
have any command line paramters"
I take that one as if you don't want to call your script with
myscript.pl -f file.txt
to read the file, and not to mean that you don't want to use parameters at all.
--
Sandman[.net]
------------------------------
Date: Thu, 05 Aug 2004 13:45:56 +0200
From: Andreas Berg <andreas.berg@ericsson.com>
Subject: How do I detect if I have incoming data in <STDIN> when I pipe something to my perl script ?
Message-Id: <cet6gg$5g5$1@newstree.wise.edt.ericsson.se>
Greetings,
I'm writing a script that is supposed to fetch information from <STDIN>
that is piped to the script, something like this:
> cat file.txt | myscript.pl
With this, I will get what I want into <STDIN> and I can read it. But,
this is the only case I want to read it. I do not want my script to try
to read from a file in case I do have any command line paramters and I
do not want the script to wait for keyboard input. If something is piped
to the script, I want that data, if nothing is piped to the script, I
dont want it to do anything with <STDIN>, how do I accomplish this ?
regards,
Andreas
------------------------------
Date: Thu, 05 Aug 2004 14:31:44 +0200
From: Andreas Berg <andreas.berg@ericsson.com>
Subject: Re: How do I detect if I have incoming data in <STDIN> when I pipe something to my perl script ?
Message-Id: <cet96a$6it$1@newstree.wise.edt.ericsson.se>
The script will have command line parameters for other things and when I
do not pipe something to the script, I dont want it to try to read these
command line parameters as if they where a file.
And, I never want the script to read from keyboard.
Having -stdin as a command line parameter would work, but it is a dirty
solution in my opinion. Doesn't look very nice.
regards,
Andreas
Sandman wrote:
> In article <cet6gg$5g5$1@newstree.wise.edt.ericsson.se>,
> Andreas Berg <andreas.berg@ericsson.com> wrote:
>
>
>>Greetings,
>>
>>I'm writing a script that is supposed to fetch information from <STDIN>
>>that is piped to the script, something like this:
>> > cat file.txt | myscript.pl
>>
>>With this, I will get what I want into <STDIN> and I can read it. But,
>>this is the only case I want to read it. I do not want my script to try
>>to read from a file in case I do have any command line paramters and I
>>do not want the script to wait for keyboard input. If something is piped
>>to the script, I want that data, if nothing is piped to the script, I
>>dont want it to do anything with <STDIN>, how do I accomplish this ?
>>
>>regards,
>>Andreas
>
>
> cat file.txt | myscript.pl -stdin
>
>
> And have myscript.pl read from <STDIN> only if $ARGV[0] is "-stdin", or use
> getopts if you want more elaborate control of the parameters. This is, if I am
> reading this line correctly:
>
> "I do not want my script to try to read from a file in case I do
> have any command line paramters"
>
> I take that one as if you don't want to call your script with
>
> myscript.pl -f file.txt
>
> to read the file, and not to mean that you don't want to use parameters at all.
>
------------------------------
Date: Wed, 4 Aug 2004 21:58:49 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: if ( $a eq 1 || $b eq 2...)
Message-Id: <tvgQc.28984$Jq2.1269919@news20.bellglobal.com>
"Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
news:vJmdnbh-yuSIGIzcRVn-jQ@speakeasy.net...
> this is starting to bother me:
>
> if (
> $req =~ /\.html$/ ||
> $req =~ /\.pl$/ ||
> $req =~ /\.shtml$/ ||
> $req =~ /\.txt$/ ||
> $req =~ /\.cgi$/ ||
> $req =~ /\.cgi\?/ ||
> $req =~ /\/\?/ ||
> $req =~ /\/$/
> ) {
> $countPage++;
> }
>
>
> there's got to be a better way.
I see three different clusters that just can't be merged. The following will
decrease the readability immensely, but will compact the code if that's all
you want:
if ( $req =~ m#(\.(s?html?|pl|cgi|txt)$)|(/[$?])|(\.(pl|cgi)\?)# ) {
$countPage++; }
Matt
------------------------------
Date: Wed, 4 Aug 2004 22:11:27 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: if ( $a eq 1 || $b eq 2...)
Message-Id: <jHgQc.29004$Jq2.1275064@news20.bellglobal.com>
"Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message
news:tvgQc.28984$Jq2.1269919@news20.bellglobal.com...
>
> "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
> news:vJmdnbh-yuSIGIzcRVn-jQ@speakeasy.net...
> > this is starting to bother me:
> >
> > if (
> > $req =~ /\.html$/ ||
> > $req =~ /\.pl$/ ||
> > $req =~ /\.shtml$/ ||
> > $req =~ /\.txt$/ ||
> > $req =~ /\.cgi$/ ||
> > $req =~ /\.cgi\?/ ||
> > $req =~ /\/\?/ ||
> > $req =~ /\/$/
> > ) {
> > $countPage++;
> > }
> >
> >
> > there's got to be a better way.
>
> I see three different clusters that just can't be merged. The following
will
> decrease the readability immensely, but will compact the code if that's
all
> you want:
>
> if ( $req =~ m#(\.(s?html?|pl|cgi|txt)$)|(/[$?])|(\.(pl|cgi)\?)# ) {
> $countPage++; }
>
Oops! For some reason I thought the $ in the last expression was escaped (I
also should have escaped it in the character class (/[\$?]), so double my
bad!):
if ( $req =~ m#(\.(s?html?|pl|cgi|txt)$)|(/\?)|(\.(pl|cgi)\?)# ) {
$countPage++; }
Matt
------------------------------
Date: 5 Aug 2004 08:47:38 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: if ( $a eq 1 || $b eq 2...)
Message-Id: <cess7a$8nm$1@mamenchi.zrz.TU-Berlin.DE>
Matt Garrish <matthew.garrish@sympatico.ca> wrote in comp.lang.perl.misc:
>
> "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
> news:vJmdnbh-yuSIGIzcRVn-jQ@speakeasy.net...
> > this is starting to bother me:
> >
> > if (
> > $req =~ /\.html$/ ||
> > $req =~ /\.pl$/ ||
> > $req =~ /\.shtml$/ ||
> > $req =~ /\.txt$/ ||
> > $req =~ /\.cgi$/ ||
> > $req =~ /\.cgi\?/ ||
> > $req =~ /\/\?/ ||
> > $req =~ /\/$/
> > ) {
> > $countPage++;
> > }
> >
> >
> > there's got to be a better way.
>
> I see three different clusters that just can't be merged. The following will
> decrease the readability immensely, but will compact the code if that's all
> you want:
>
> if ( $req =~ m#(\.(s?html?|pl|cgi|txt)$)|(/[$?])|(\.(pl|cgi)\?)# ) {
> $countPage++; }
How about a compromise (untested):
$countPage ++ if
$req =~ /\.(?:html|pl|shtml|txt|cgi)$/ ||
$req =~ /\.cgi\?/ ||
$req =~ /\/\?/ ||
$req =~ /\/$/;
or even
/\.(?:html|pl|shtml|txt|cgi)$/ || /\.cgi\?/ || /\/\?/ || /\/$/ and
$countPage ++ for $req;
Anno
------------------------------
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 V10 Issue 6836
***************************************