[22591] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4812 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Apr 3 18:13:08 2003

Date: Thu, 3 Apr 2003 15:10:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 3 Apr 2003     Volume: 10 Number: 4812

Today's topics:
    Re: looking for duplicates <spamReallySucks@forgetit.com>
        open COMMAND with a pipe (J. VerSchave)
    Re: open COMMAND with a pipe <glex_nospam@qwest.net>
        Optimizing Search Routine <lyvoise@aol.com>
    Re: Optimizing Search Routine <tassilo.parseval@rwth-aachen.de>
    Re: Perl One-Liner <spamtrap@nowhere.com>
        Problem with redirecting STDOUT and STDERR (Scott)
    Re: Problem with redirecting STDOUT and STDERR <grazz@nyc.rr.com>
        SIGINT and SIGQUIT with system() <me@privacy.net>
    Re: SIGINT and SIGQUIT with system() <grazz@nyc.rr.com>
        sort numerically descending is not right? <bing-du@tamu.edu>
        Stumped! <delautenschl@[nospam]wisc.edu>
    Re: Stumped! <skuo@mtwhitney.nsc.com>
    Re: Stumped! <perl-dvd@darklaser.com>
        system PROGRAM LIST syntax <skuo@mtwhitney.nsc.com>
        Text::ParseWords <Use-Author-Supplied-Address-Header@[127.1]>
        Using useradd and groupadd under Perl <stephanj@melbpc.org.au>
    Re: Using useradd and groupadd under Perl <tony_curtis32@yahoo.com>
    Re: Which is better - hashes or subroutines <Jodyman@hotmail.com>
    Re: why heredoc? was: Re: print here-documents question <mpapec@yahoo.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 03 Apr 2003 21:03:21 GMT
From: "Scaramouche" <spamReallySucks@forgetit.com>
Subject: Re: looking for duplicates
Message-Id: <tk1ja.95814$o8.1688147@twister.tampabay.rr.com>

thank you to all that replied.
as always, this ng is always willing to help other less experienced perl
users.  the code below seems to work but i've not had the chance to test it
yet.

open(INFILE, "infile.txt") or die "Error (infile): $!";
my (%seen);
while( <INFILE> )
   {
      ++$_ for @seen{/\d+/g};
   }
foreach (sort {$a == $b} keys %seen)
   {
      print "$_ was a duplicate\n" if $seen{$_} > 1;
   }
close INFILE;

one last quick question though: if i wanted to print out those numbers that
are NOT duplicates within the flat text file...
 ...would i simply change the '==' to '!=' within the foreach loop?




"Scaramouche" <spamSucks@forgetIt.com> wrote in message
news:v0Nia.1111$gF2.12@nwrddc03.gnilink.net...
> i think i know how to open a file and read each line in it:
>
> open(INFILE, "in.txt") or die "Error (in): $!";
>
> while(<INFILE>){
> }
>
> but how can i return in some way duplicates found.  i'm trying to find
> duplicate numbers within the file. this file however, also has some text
> within it.
>
> thanks
>
>
>




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

Date: 3 Apr 2003 09:28:54 -0800
From: carlisle411@yahoo.com (J. VerSchave)
Subject: open COMMAND with a pipe
Message-Id: <c5b7bb8c.0304030928.3fd34a79@posting.google.com>

So I am trying to do something like this:

open COMMAND, "make -f Makefile |";
while(<COMMAND>)
{
  print;
}

close COMMAND;

But now I need to check the return value of 'make.'   I need to know
if it was successful or if it failed.  But I don't know how to do
this.  I tried combinations of '$?' but that doesn't seem to work. 
Ideas?  Thanks.

-j


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

Date: Thu, 03 Apr 2003 12:20:23 -0600
From: Jeff D Gleixner <glex_nospam@qwest.net>
Subject: Re: open COMMAND with a pipe
Message-Id: <HU_ia.43$uo6.52008@news.uswest.net>

J. VerSchave wrote:
> So I am trying to do something like this:
> 
> open COMMAND, "make -f Makefile |";
> while(<COMMAND>)
> {
>   print;
> }
> 
> close COMMAND;
> 
> But now I need to check the return value of 'make.'   I need to know
> if it was successful or if it failed.  But I don't know how to do
> this.  I tried combinations of '$?' but that doesn't seem to work. 
> Ideas?  Thanks.
> 
> -j

Easier to use system.

system("/usr/bin/make -f ./Makefile") == 0 or die "make failed!";

If you need the error, you could capture STDERR.

 From perlfaq8:

To capture a command's STDERR but discard its STDOUT:

     $output = `cmd 2>&1 1>/dev/null`;           # either with backticks
     $pid = open(PH, "cmd 2>&1 1>/dev/null |");  # or with an open pipe
     while (<PH>) { }                            #    plus a read



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

Date: Thu, 03 Apr 2003 21:21:23 GMT
From: "Lyvoise" <lyvoise@aol.com>
Subject: Optimizing Search Routine
Message-Id: <nB1ja.10008$n2.3504@clmboh1-nws5.columbus.rr.com>

I have a flat file text database which requires a match of keywords (single
keyword and multiple keyword scenarios).  The database is easily 10,000
records and each record must be checked to see if it matches a keyword or
keywords entered.

I can easily setup a scenario where I open the database file, read it using
the while (<DATABASE>) and doing a foreach keyword if ($line eq "keyword")
push it to a new arrary of the matched results. Example:

open(DATA, "database.dat");
while (<DATA>)
    {
    foreach $keyword (@keywords) {
    if ($_ =~ m/\b$keyword\b/i) {
    push (@my_results, $_);
    }

    }
close(DATA);

Each line of data is approimately 30 database fields logn with each
datafield mostly being a single word to match.

This works without a problem but I'm sure there must be some more efficient
way of searching a flat file text database.  Any suggestions on improving
the performance of such a standard routine.

I also will have other scenarios where a more advanced search will be
required and have to match mulitple values for specific database fields but
and have used a similar routine and then further narrows the results file by
spliting the data fields and checking to see if the line still matches the
criteria and if it does keep it otherwise disgard it from the arrary.

Any suggestions to help optimize these scenarios would be greatly
appreciated.  I must stick with the current database format.

Thanks in advance for any input.




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

Date: 3 Apr 2003 22:31:58 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Optimizing Search Routine
Message-Id: <b6icou$3dl$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Lyvoise:

> I have a flat file text database which requires a match of keywords (single
> keyword and multiple keyword scenarios).  The database is easily 10,000
> records and each record must be checked to see if it matches a keyword or
> keywords entered.
> 
> I can easily setup a scenario where I open the database file, read it using
> the while (<DATABASE>) and doing a foreach keyword if ($line eq "keyword")
> push it to a new arrary of the matched results. Example:
> 
> open(DATA, "database.dat");
> while (<DATA>)
>     {
>     foreach $keyword (@keywords) {
>     if ($_ =~ m/\b$keyword\b/i) {
>     push (@my_results, $_);
>     }
> 
>     }
> close(DATA);
> 
> Each line of data is approimately 30 database fields logn with each
> datafield mostly being a single word to match.
> 
> This works without a problem but I'm sure there must be some more efficient
> way of searching a flat file text database.  Any suggestions on improving
> the performance of such a standard routine.

You are matching each line against a number of patterns repeatedly.
Those are the same patterns and yet perl has to compile them each time.
To avoid that, simply compile them once and use them in their compiled
form:

    @keywords = map { qr/\b$_\b/i } @keywords;
    
    while ... {
        foreach my $keyword (@keywords) {
            push @my_results, $_ if /$keyword/;
        }
    ...

This should already result in a nice speed-up.

> I also will have other scenarios where a more advanced search will be
> required and have to match mulitple values for specific database fields but
> and have used a similar routine and then further narrows the results file by
> spliting the data fields and checking to see if the line still matches the
> criteria and if it does keep it otherwise disgard it from the arrary.

Sorry, don't understand this paragraph.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Thu, 03 Apr 2003 12:33:31 -0500
From: Andrew Lee <spamtrap@nowhere.com>
Subject: Re: Perl One-Liner
Message-Id: <u2so8vs6fjpqu4d7gv8790gdebm0b5jgga@4ax.com>

On Wed, 02 Apr 2003 17:28:21 GMT, "Buck Turgidson" <jc_va@hotmail.com>
wrote:

>I found a perl one-liner to print the os block size on Unix.  How can I tell
>what is the range of the other subscripts, and their meaning?
>
>
>
>perl -e '$a=(stat ".")[11]; print $a'
>

perldoc -f stat



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

Date: 3 Apr 2003 13:00:30 -0800
From: googlegroups@scottsavarese.com (Scott)
Subject: Problem with redirecting STDOUT and STDERR
Message-Id: <677a147d.0304031300.4cbfe807@posting.google.com>

I want to redirect standard out and stderr into a Log module that I
created. I created a Log.pm and performed the following tie:

# Redirect STDERR and STDOUT to logfile
tie( *STDOUT, 'Log', $syswatch->getval( 'logfile' ), 
     $syswatch->getval( 'logfilerotateinterval' ) )
    or die "Can't tie variable: $!\n"; 
autoflush STDOUT 1;
*STDERR = *STDOUT;

I can do a print "blah" and a print STDERR "blah" and both go to the
Log module correctly. However here is the problem. I have insertable
modules that a user can add to the program by modifying a configfile.
Perl will run the code, and because it is user code, sometimes it
generates errors, like the following:

Undefined subroutine &Plugins::Foo::_do_check called at
lib/Plugins/Foo.pm line 48.

The problem is that this message is not getting redirected to the Log
module. I guess it is because the error message is generated by perl
and not by the program. The question is... How can I redirect those
errors to that Log module I wrote...

Of course the simple solution is to go ./program 2>&1 | ./logprogram
however I would like to avoid doing that where possible.

Thanks,
Scott


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

Date: Thu, 03 Apr 2003 21:28:39 GMT
From: Steve Grazzini <grazz@nyc.rr.com>
Subject: Re: Problem with redirecting STDOUT and STDERR
Message-Id: <bI1ja.5324$an1.814@twister.nyc.rr.com>

Scott <googlegroups@scottsavarese.com> wrote:
> I want to redirect standard out and stderr into a Log 
> module that I created. I created a Log.pm and performed 
> the following tie:

[snip]

> The problem is that this message is not getting redirected 
> to the Log module. I guess it is because the error message 
> is generated by perl and not by the program. The question is... 
> How can I redirect those errors to that Log module I wrote...
> 

  $SIG{__WARN__} = sub { print STDERR @_ };

-- 
Steve


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

Date: Thu, 03 Apr 2003 21:11:36 +0200
From: Sven <me@privacy.net>
Subject: SIGINT and SIGQUIT with system()
Message-Id: <me-36992C.21113603042003@news.fu-berlin.de>

Hi there

I'm stuck with system(). According to some posts, system() is passing 
SIGINT and SIGQUIT to it's child, yet this doesn't seem to work. I've 
boiled the code down to the following lines. (Please note that 
'alsaplayer' is a console soundfile player that honors SIGINT and 
SIGQUIT by immediately quitting.)

sub controler
{
    my $pid = fork;
    if (!$pid) { &player(); }
    while (!-e "stop") { sleep(10); }
    kill('QUIT', $pid);
}

sub player
{
    system("alsaplayer -q song.mp3");
    sleep;
}

The controler forks off the player child which system()-implicitly forks 
again and runs alsaplayer in the child. So the sound's playing. I 'touch 
stop' and the controler signals SIGQUIT to the player child. This works 
as I can see when I catch SIGQUIT there. Still, the SIGQUIT is not 
forwarded to the child running alsaplayer the way it is supposed to do - 
at least according to my literature.

Am I missing something here? Any idea how I could do this differently?

Thanks for your hints,    Steve


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

Date: Thu, 03 Apr 2003 21:04:37 GMT
From: Steve Grazzini <grazz@nyc.rr.com>
Subject: Re: SIGINT and SIGQUIT with system()
Message-Id: <Fl1ja.3726$n95.1243961@twister.nyc.rr.com>

Sven <me@privacy.net> wrote:
> 
> I'm stuck with system(). According to some posts, 
> system() is passing SIGINT and SIGQUIT to it's child, 
> yet this doesn't seem to work.

Those posts could be wrong (it happens) or you might have
misunderstood them.  But the docs for system state that
SIGINT and SIGQUIT will be blocked, not "passed on".

    $ perldoc -f system

    [...] Because system() and backticks block SIGINT and 
    SIGQUIT, killing the program they're running doesn't 
    actually interrupt your program.

The bit about "killing the program they're running" refers 
to keyboard signals, e.g. ^C from a terminal:

    $ perl -le 'system qw(sleep 5); print "Done: ", $? & 127'
    ^CDone: 2

Here the perl process and the child process are both sent
SIGINT by the kernel because they're in the foreground 
process group.  Perl blocks it, but the child doesn't.

In your code, you send SIGQUIT directly to the fork'd
Perl process.  That Perl has blocked it, and nobody has
signalled the "alsaplayer" child.

The short answer is "just use exec()"...

> I've boiled the code down to the following lines. (Please 
> note that 'alsaplayer' is a console soundfile player that 
> honors SIGINT and SIGQUIT by immediately quitting.)
> 
> sub controler
> {
>     my $pid = fork;

You should check the success of fork():

  sub controller {
      defined( my $pid = fork )
        or die "Couldn't fork: $!";

>     if (!$pid) { &player(); }
                   ^

Don't use the old-style "&" function call unless you 
know what it does and need the old-style behavior.

[ What it does is documented in "perlsub", and you 
  certainly don't need the prototype-disabling here. ]

      if ($pid == 0) { player() }

>     while (!-e "stop") { sleep(10); }
>     kill('QUIT', $pid);
> }

You need some more error-handling and a wait() here.

> sub player
> {
>     system("alsaplayer -q song.mp3");
>     sleep;

And here you just use exec.

  sub player {
      exec qw(alsaplayer -q song.mp3);
      die "Couldn't exec alsaplayer: $!"; 
  }

HTH
-- 
Steve


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

Date: Thu, 03 Apr 2003 16:22:54 -0600
From: Bing Du Test <bing-du@tamu.edu>
Subject: sort numerically descending is not right?
Message-Id: <3E8CB43E.F2D7C2A3@tamu.edu>

Script is like this:

=======
#!/usr/local/bin/perl

@terms =
('2003V','20033','20032','20031','20023','2004A','2003B','2003C','2002V','2002C');

foreach $term (sort {$b <=> $a} @terms)
      {
        push @terms_in_order, $term;
      }

print "terms_in_order are ",join(',',@terms_in_order),"\n";
======

The result is:

=======
terms_in_order are
20033,20032,20031,20023,2004A,2003B,2003C,2003V,2002C,2002V
=======

But what I expect should be:

======
terms_in_order are
2004A,2003V,2003C,2003B,20033,20032,20031,2002V,2002C,20023
======

Can anybody tell me why @terms is not sorted?  Is it because some
elements have alphabetic characters?

Thanks for any help.


Bing



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

Date: Thu, 03 Apr 2003 13:36:03 -0600
From: pooba53 <delautenschl@[nospam]wisc.edu>
Subject: Stumped!
Message-Id: <b6i2f1$bha$1@news.doit.wisc.edu>

I have a hash that has multiple values associated with a single key.
i.e.:

key is "CG2349"

values associate with "CG2349" are :

(1,2,3,4,5)
(2,3,4,5,6,7)
(10,11,12,13)

Since I have multiple values associated with a single key, I'm using
references.

I'm trying to get each value (the three above) into a single array so I
can extract unique elements from the "merge".

The problem I'm having is how to do this and create a new hash with one
value associated with each key after merging and "uniquing".

I'm pretty sure I need to use the each function such as:

while (($key, $value) = each(${@MyArray})  {

}


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

Date: Thu, 3 Apr 2003 12:28:59 -0800
From: Steven Kuo <skuo@mtwhitney.nsc.com>
Subject: Re: Stumped!
Message-Id: <Pine.GSO.4.21.0304031225270.28493-100000@mtwhitney.nsc.com>

On Thu, 3 Apr 2003, pooba53 wrote:

> I have a hash that has multiple values associated with a single key.
> i.e.:
> 
> key is "CG2349"
> 
> values associate with "CG2349" are :
> 
> (1,2,3,4,5)
> (2,3,4,5,6,7)
> (10,11,12,13)
> 
> Since I have multiple values associated with a single key, I'm using
> references.



So I imagine that you have a hash structure similar to this:

my %hash = (
    CG2349 => [ [1,2,3,4,5], [2,3,4,5,6,7], [10,11,12,13] ],
);

 
> I'm trying to get each value (the three above) into a single array so I
> can extract unique elements from the "merge".
> 
> The problem I'm having is how to do this and create a new hash with one
> value associated with each key after merging and "uniquing".
> 
> I'm pretty sure I need to use the each function such as:
> 
> while (($key, $value) = each(${@MyArray})  {
> 



each expects a hash, not a scalar.

You may want to try something like this:

#! /usr/local/bin/perl
use strict;
use warnings;

my %hash = (
    CG2349 => [ [1,2,3,4,5], [2,3,4,5,6,7], [10,11,12,13] ],
);

while (my ($key,$aref) = each %hash) {
    my %seen;
    my @unique = grep !$seen{$_}++, map @$_, @$aref;
    print "unique elements for $key : @unique\n";
}


See also

% perldoc -q 'remove duplicate elements'

-- 
Hope this helps,
Steven



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

Date: Thu, 03 Apr 2003 20:38:45 GMT
From: "David" <perl-dvd@darklaser.com>
Subject: Re: Stumped!
Message-Id: <pZ0ja.39$NY5.15700@news-west.eli.net>

"pooba53" <delautenschl@[nospam]wisc.edu> wrote in message
news:b6i2f1$bha$1@news.doit.wisc.edu...
> I have a hash that has multiple values associated with a single key.
> i.e.:
>
> key is "CG2349"
>
> values associate with "CG2349" are :
>
> (1,2,3,4,5)
> (2,3,4,5,6,7)
> (10,11,12,13)
>
> Since I have multiple values associated with a single key, I'm using
> references.
>
> I'm trying to get each value (the three above) into a single array so
I
> can extract unique elements from the "merge".
>
> The problem I'm having is how to do this and create a new hash with
one
> value associated with each key after merging and "uniquing".
>
> I'm pretty sure I need to use the each function such as:
>
> while (($key, $value) = each(${@MyArray})  {
>
> }

Ok, so what it looks like is, you have a var structure like this:

$hash{'CG2349'} = [
    [1, 2, 3, 4, 5],
    [2, 3, 4, 5, 6, 7],
    [10, 11, 12, 13]
];

So your hash element points to an array ref which contains 3 array
ref's.
Under that circumstance, you could do something like this which would
both combine your three lists into one as well as cause your list to be
unique.

my %newlist; # define a new hash
# loop through the 3 array refs
foreach my $arrayref (@{$hash{'CG2349'}}){
    # loop through each element in this array ref
    # assign it as a key in the %newlist hash
    $newlist{$_} = 1 for (@{$arrayref});
}

After you have done this, because keys are unique in a hash, you now
have a unique list, and they are all in just one list.  Beware if you
have text as your data, keys in a hash are case sensitive, so 'This' is
different than 'this'.

Regards,
David
perl -e'print for map chr$_+1,"111100113107044099117099132"=~/(.{3})/g'




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

Date: Thu, 3 Apr 2003 12:12:45 -0800
From: Steven Kuo <skuo@mtwhitney.nsc.com>
Subject: system PROGRAM LIST syntax
Message-Id: <Pine.GSO.4.21.0304031204170.28493-100000@mtwhitney.nsc.com>


In line 12 below, I'm issuing a system call with an indirect
object in front of the argument list.

% cat -n twoface.pl
     1  #! /usr/bin/perl
     2  use strict;
     3  use warnings;
     4  use FindBin;
     5  require 5.008;
     6
     7  my ($calledname) = ($0 =~ /twoface.pl$/)? 'twoface.pl' : 'somethingelse';
     8
     9  if ($calledname eq 'twoface.pl') {
    10      printf "$calledname sees umask as %04o\n",umask; # nominally 0022
    11      my $indirect = "$FindBin::Bin/$calledname";
    12      system {$indirect} 'somethingelse'; # use indirect object
    13  } else {
    14      umask 0002;
    15      printf "$calledname sees umask as %04o\n",umask; 
    16  }
    17


When I ran the program, I expected to see something like:
% ./twoface.pl
twoface.pl sees umask as 0022
somethingelse sees umask as 0002


Instead, I see:
% ./twoface.pl
twoface.pl sees umask as 0022
twoface.pl sees umask as 0022
twoface.pl sees umask as 0022
twoface.pl sees umask as 0022
twoface.pl sees umask as 0022
twoface.pl sees umask as 0022
(ad. infinitum)


Do I have an misunderstanding of how the
system PROGRAM LIST syntax works?

By the way, my version of perl is:
% \perl -v

This is perl, v5.8.0 built for i386-linux-thread-multi

-- 
Thanks,
Steve



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

Date: Thu, 3 Apr 2003 09:41:06 -0800
From: Robert Brooks <Use-Author-Supplied-Address-Header@[127.1]>
Subject: Text::ParseWords
Message-Id: <200304031741.h33Hf68h009061@www.aarg.net>


In the following script, &shellwords returns an array
of three elements.  I would like to know what the first
element is, that is element number 0.

Can you tell me how to print out the element in question?
Or, could you supply some code that would print the character
out in hexadecimal or binary?

Thanks for any help you might give.

# ----------- script ----------

#!/usr/bin/perl
#
use warnings;
use strict;
use Text::ParseWords;
my $string = ' alpha beta';

my @words = &shellwords($string);

my $z = $#words; # Get index number of last element

print "Last element number is: $z \n";


foreach my $w (@words) {
  print $w, "\n";
}

print "==============================\n";

my @copy = @words; # Make a copy
if ($copy[0] eq 0x20) {shift @copy} # Get rid of ?

foreach my $c (@copy) {
  print $c, "\n";
}





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

Date: Fri, 4 Apr 2003 08:32:39 +1000
From: "stephanj" <stephanj@melbpc.org.au>
Subject: Using useradd and groupadd under Perl
Message-Id: <b6icr6$rc7$1@possum.melbpc.org.au>

Hi all,

What would be the easiest way to create a Perl script that uses these 2
Linux
commands?





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

Date: Thu, 03 Apr 2003 16:34:55 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Using useradd and groupadd under Perl
Message-Id: <8765pv1a7k.fsf@limey.hpcc.uh.edu>

>> On Fri, 4 Apr 2003 08:32:39 +1000,
>> "stephanj" <stephanj@melbpc.org.au> said:

> Hi all, What would be the easiest way to create a Perl
> script that uses these 2 Linux commands?

with an editor.

I think you'll need to supply more information to get a
sane answer.

hth
t



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

Date: Thu, 03 Apr 2003 16:14:32 GMT
From: "Jodyman" <Jodyman@hotmail.com>
Subject: Re: Which is better - hashes or subroutines
Message-Id: <I5Zia.7165$4P1.560451@newsread2.prod.itd.earthlink.net>


"Matthew Braid" <mbear@uq.net.au> wrote in message

> Hi all,
>
> I've written a small package to handle multiple languages in a GUI
> (using perl/tk). It does this by loading a set of tags/values from a
> config file (in the form of PHRASENAME = text in whatever language).
[snip]
> This has a bonus of not allowing people to reach into the code and
> change the phrases (which is very easy for the current system), but I
> dont't know if this is more/less efficient, considered bad coding
> practice etc.

How about a class?

perldoc perltoot

Jody




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

Date: Thu, 03 Apr 2003 21:55:13 +0200
From: Matija Papec <mpapec@yahoo.com>
Subject: Re: why heredoc? was: Re: print here-documents question
Message-Id: <em2p8vsg67klhkr5nh4dknlca9jqe0u8v5@4ax.com>

Benjamin Goldberg <goldbb2@earthlink.net> wrote:
>> >That depends on what is in the string that you are quoting.
>> >
>> >I think this is harder to read than the above:
>> >
>> >  print "var newLine = unescape(\"%0A\")";
>> >  print "alert(\"The correct email format is : \"  + newLine + \"username\@domain.extension\")";
>> >
>> >
>> >Here docs are WYSIWYGish.
>> 
>> Yes, but Perl quoting mechanism is even more WYSIWYG!(qq{} q{})
>
>But the q and qq operators can only take a single character delimiter.

Which is good enough for most occasions, but you're right about large text
chunks, so it's better to be on the safe side.

>And, if you've a multi-line string, and *don't* want the first line to
>be newline, then you've got to write something like:
>
>   $x = q[blah blah
>blah
>asdfasdf
>];
>
>Which is less pretty than the the equvilant here-doc:
>
>   $x = <<'EOS';
>blah blah
>blah
>asdfasdf
>EOS

I guess this one falls into more subjective categorie; I would take first
one as it looks simple and cleaner.

>> Here doc in great manner resembles shell scripting
>
>Where do you think it came from? :)

IMHO, it came from desire to attract shell scripting guys to write their
stuff in Perl. ;)



-- 
Matija


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

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.  

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


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