[29585] in Perl-Users-Digest
Perl-Users Digest, Issue: 829 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Sep 7 11:10:12 2007
Date: Fri, 7 Sep 2007 08:09:09 -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 Fri, 7 Sep 2007 Volume: 11 Number: 829
Today's topics:
debugging and flow control <alexxx.magni@gmail.com>
Re: debugging and flow control <mritty@gmail.com>
Re: Ensuring parent/child processes <jrpfinch@gmail.com>
Need to change icons on 400 + shortcuts <cjohnsonuk@googlemail.com>
PERL INCLUDE PATH automatically from Java Code eser@libero.it
Re: PERL INCLUDE PATH automatically from Java Code <klaus03@gmail.com>
Re: Reduce CPU time while polling serial port (DF4OR)
Regular Expression <fritz-bayer@web.de>
Re: Regular Expression <klaus03@gmail.com>
Re: Regular Expression <benoit.lefebvre@gmail.com>
Re: Regular Expression <fritz-bayer@web.de>
Switching with case in perl ? <lerameur@yahoo.com>
Re: Switching with case in perl ? <mritty@gmail.com>
Re: trying to export some utility methods, and struggli <paduille.4061.mumia.w+nospam@earthlink.net>
Re: trying to export some utility methods, and struggli <bugbear@trim_papermule.co.uk_trim>
variable running once <lerameur@yahoo.com>
Re: Warning about unused lexical variables <hjp-usenet2@hjp.at>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 07 Sep 2007 04:33:05 -0700
From: "alexxx.magni@gmail.com" <alexxx.magni@gmail.com>
Subject: debugging and flow control
Message-Id: <1189164785.466611.159220@w3g2000hsg.googlegroups.com>
Hi all, I need some advice:
in the past, when writing programs I often used, to help me in
debugging, the following style:
my $debuglevel=1;
....
print("var value is $a\n") if ($debuglevel>0);
....
print(LOGFILE "array values are @x\n") if ($debuglevel>2);
...
you get the idea.
Now I'm working on a project very computational-intensive, and I'm
worried about the many lines "if ($debuglevel...)" to be evaluated.
I was wandering: if $debuglevel was a constant, not a variable, it is
possible that the program, when launched, evaluates those lines from
the beginning? In this case I should not worry about later evaluation.
Any other suggestion for flow control?
thanks!
Alessandro Magni
------------------------------
Date: Fri, 07 Sep 2007 07:46:17 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: debugging and flow control
Message-Id: <1189176377.864936.15680@k79g2000hse.googlegroups.com>
On Sep 7, 7:33 am, "alexxx.ma...@gmail.com" <alexxx.ma...@gmail.com>
wrote:
> my $debuglevel=1;
> ....
> print("var value is $a\n") if ($debuglevel>0);
> ....
> print(LOGFILE "array values are @x\n") if ($debuglevel>2);
> ...
>
> you get the idea.
> Now I'm working on a project very computational-intensive, and I'm
> worried about the many lines "if ($debuglevel...)" to be evaluated.
>
> I was wandering: if $debuglevel was a constant, not a variable,
> it is possible that the program, when launched, evaluates those
> lines from the beginning? In this case I should not worry about
> later evaluation.
That would appear to be accurate....
$ perl -MO=Deparse -e'
my $x = 1;
print "Hello " if $x;
print "World\n" if $x;
'
my $x = 1;
print 'Hello ' if $x;
print "World\n" if $x;
-e syntax OK
$ perl -MO=Deparse -e'
use constant x => 1;
print "Hello " if x;
print "World\n" if x;
'
use constant ('x', 1);
print 'Hello ';
print "World\n";
-e syntax OK
However, I'm not even a little bit convinced that the savings you'll
see from this will be noticeable. I'd try a Benchmark if I were you,
to see if it's worth your time to go through and change your code...
Paul Lalli
------------------------------
Date: Fri, 07 Sep 2007 04:22:32 -0700
From: jrpfinch <jrpfinch@gmail.com>
Subject: Re: Ensuring parent/child processes
Message-Id: <1189164152.965906.285980@y42g2000hsy.googlegroups.com>
How does this look? The only thing I am worried about is if another
external process happens to inherit the pid of one of the processes I
am about to kill (i.e. it has died already). Is there any way of
making sure the kill -9 is killing a child process and not killing
some other process (this is obviously tiny likelihood but you never
know!)
#!/opt/perl5.8.8/bin/perl
#-------------------------------------------------------------------------------
# Interpreter settings
#-------------------------------------------------------------------------------
use warnings;
use strict;
my $childOnePid;
my $childTwoPid;
my $diePid;
print "Parent process pid=$$\n";
unless (defined ($childOnePid = fork))
{
die "cannot fork: $!";
}
#-------------------------------------------------------------------------------
# Child process loops for 50 seconds
# How to ensure this stops if the parent process dies unexpectedly?
#-------------------------------------------------------------------------------
unless ($childOnePid)
{
print "Inside childOne pid=$$\n";
my @packages = ("Schedule::Cron",
"SOAP::Lite",
"Log::Log4perl",
"MetaMon::MetaMonConfigLoader",
"MetaMon::PhaseOne",
"MetaMon::GetEnvVar",
"Data::Dumper");
my $package;
eval {
require "PAR.pm";
import PAR q(/opt/perl5.8.8/lib/site_perl/*.par);
for $package (@packages)
{
(my $pkg = $package) =~ s|::|/|g; # require need a path
require "$pkg.pm";
import $package;
}
};
die $@ if( $@ );
for (0..10)
{
sleep 1;
}
print "About to exit childOne\n";
exit;
}
unless (defined ($childTwoPid = fork))
{
die "cannot fork: $!";
}
#-------------------------------------------------------------------------------
# Child process loops for 50 seconds
# How to ensure this stops if the parent process dies unexpectedly?
#-------------------------------------------------------------------------------
unless ($childTwoPid)
{
print "Inside childTwo pid=$$\n";
my @packages = ("Schedule::Cron",
"SOAP::Lite",
"Log::Log4perl",
"Data::Dumper");
my $package;
eval {
for $package (@packages)
{
(my $pkg = $package) =~ s|::|/|g; # require need a path
require "$pkg.pm";
import $package;
}
};
die $@ if( $@ );
for (0..12)
{
sleep 1;
}
print "About to exit childTwo\n";
exit;
}
sleep 10;
$diePid = wait();
if ($diePid == $childOnePid)
{
print "Killing child two pid\n";
kill 9, $childTwoPid;
}
elsif ($diePid == $childTwoPid)
{
print "Killing child one pid\n";
kill 9, $childOnePid;
}
elsif ($diePid == -1)
{
#both already dead
sleep 0;
}
else
{
print "Should never get here\n";
}
print "finished\n";
__END__
------------------------------
Date: Fri, 07 Sep 2007 12:45:48 -0000
From: cjohnsonuk <cjohnsonuk@googlemail.com>
Subject: Need to change icons on 400 + shortcuts
Message-Id: <1189169148.077709.162950@57g2000hsv.googlegroups.com>
We have menus with 400 + shortcuts on them that are on a network
drive. We use folder redirection in a group policy in the AD to get
all PCs to look here for their menus. We are looking for a better way
as there is a 10+ second delay after clicking start|programs before
anything displays) but until then I need to do two things
1) check that the path to the icons file is to a valid file
2) change the path to a local .ico file with all the icons in it.(and
eventually set up the menus to be copied locally too) (Can't use
mandatory profiles as users require cached details to log in to
laptops at home.)
I've used the following code that I've copied /combined off the net to
walk the program files directory tree and return the icon for
each .lnk file it finds.
It has two problems
1) Not all folders are recognised as folders. There doesn't seem to
be a pattern here. I can't see why some are listed as DIRs and some
as FILEs. Those recognised as files obviously don't get walked.
2) I think the read_shortcut subroutine requires an absolute path but
my current working directoy (&cwd) only appears to return the parent
dir.
I'm running v5.8.8 built for MSWin32-x86-multi-thread on WIndows XP
SP2
The program files are on a windows share on a 2k3 server.
Any ideas appreciated.
Output
======================
DIR>> Business & Economics
LNK>> Business Plan.lnk
path : Y:/StartMenu/Programs/Business & Economics/Business Plan.lnk
Icon : C:\WINDOWS\explorer.exe,10
LNK>> Economics Today Volumes 7 8 and 9.lnk
path : Y:/StartMenu/Programs/Economics Today Volumes 7 8 and 9.lnk
Icon : ,0
LNK>> Go Chancellor!!!.lnk
path : Y:/StartMenu/Programs/Go Chancellor!!!.lnk
Icon : ,0
LNK>> Nuffield BP.lnk
path : Y:/StartMenu/Programs/Nuffield BP.lnk
Icon : ,0
FILE CorelDRAW Graphics Suite 12
======================
======================
Code
======================
#!/usr/bin/perl -s
use Win32::OLE;
use Cwd; # module for finding the current working directory
my $wsh = new Win32::OLE 'WScript.Shell';
sub read_shortcut {
my $lnk_path = shift; # path of existing .lnk file
my $shcut = $wsh->CreateShortcut($lnk_path) or die;
# $shcut->TargetPath
$shcut->IconLocation
}
# This subroutine takes the name of a directory and recursively scans
# down the filesystem from that point looking for files named "core"
sub ScanDirectory{
my ($workdir) = shift;
my ($startdir) = &cwd; # keep track of where we began
chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
my @names = readdir(DIR) or die "Unable to read $workdir:$!\n";
closedir(DIR);
foreach my $name (@names){
next if ($name eq ".");
next if ($name eq "..");
if (-d $name){ # is this a directory?
print "DIR>> ".$name."\n";
# $name =~ s/ /\\ /;
# print ">>DIR ".$name."\n";
&ScanDirectory($name);
next;
}
if ($name =~ m/.lnk/) { # does this filename
have .lnk in it
print "LNK>> ".$name."\n";
$mypath=&cwd."/".$name;
print "path : ".$mypath."\n";
print " Icon : ".read_shortcut("$mypath")."\n";
}
else {
print " FILE ".$name."\n";
}
chdir($startdir) or
die "Unable to change to dir $startdir:$!\n";
}
}
&ScanDirectory("Y:\\StartMenu");
======================
------------------------------
Date: Fri, 07 Sep 2007 06:59:10 -0700
From: eser@libero.it
Subject: PERL INCLUDE PATH automatically from Java Code
Message-Id: <1189173550.778979.187490@50g2000hsm.googlegroups.com>
Hi all!
I have a big problem..
I developed an eclipse plugin who creates projects of "perl nature"
with a particular path: I must modify the perl build path but I don't
know what I have to do...In particular if I create the perl nature
project now as now, it is created with the original perl build path
and if I want to modify it I have to do right click on perl project
that is create starting the plugin, then "Properties" and then "Perl
Include Path", but I don't have to do this with my hands from the perl
project view. The plugin must be done in a way that the perl project
created must have the classpath already setted. If I don't modify this
path from the java code, a module (file.pm, is a mine particular
module who requires a particular path because call different USE in
his package) that is loaded at the same time of the perl project
returns errors because of some USE in file.pm .... who are not able to
find the right path
My java code now is:
IWorkspace work=ResourcesPlugin.getWorkspace();
IWorkspaceRoot workRoot=ResourcesPlugin.getWorkspace().getRoot();
IProject newProject=workRoot.getProject(name);
IProjectDescription
newProjectDescription=work.newProjectDescription(name);
String[] natureId=new String[1];
natureId[0]="org.epic.perleditor.perlnature"; //PERL NATURE
newProjectDescription.setNatureIds(natureId);
..............
//Here I think is the place where I have to modify the perl path of
the project the plugin is goig to create
........................
newProject.create(newProjectDescription,null); //creazione progetto
Can you give me an idea or a little piece of source code to resolve
this problem?
I browse a lot in the web but I didn't find any idea to continue...
Is it possible to look throught perl path from java code?
Thanks very much! I need some helps ..!
Dani
------------------------------
Date: Fri, 07 Sep 2007 07:24:26 -0700
From: Klaus <klaus03@gmail.com>
Subject: Re: PERL INCLUDE PATH automatically from Java Code
Message-Id: <1189175066.413401.219740@k79g2000hse.googlegroups.com>
On Sep 7, 3:59 pm, e...@libero.it wrote:
[ snip ]
> ...creates projects of "perl nature"
> with a particular path: I must modify the perl build path but I don't
> know what I have to do...
[ snip ]
> a module (file.pm, is a mine particular
> module who requires a particular path because call different USE in
> his package) that is loaded at the same time of the perl project
> returns errors because of some USE in file.pm .... who are not able to
> find the right path
>
> My java code now is:
[ snip java ]
I don't know anything about Eclipse or Java, but as far as Perl is
concerned, you could read about "@INC" in "perldoc perlvar":
=======================
@INC
The array @INC contains the list of places that the do EXPR, require,
or use constructs look for their library files. It initially consists
of the arguments to any -I command-line switches, followed by the
default Perl library, probably /usr/local/lib/perl, followed by ``.'',
to represent the current directory. (``.'' will not be appended if
taint checks are enabled, either by -T or by -t.) If you need to
modify this at runtime, you should use the use lib pragma to get the
machine-dependent library properly loaded also:
use lib '/mypath/libdir/';
use SomeMod;
You can also insert hooks into the file inclusion system by putting
Perl code directly into @INC. Those hooks may be subroutine
references, array references or blessed objects. See require in the
perlfunc manpage for details.
=======================
--
Klaus
------------------------------
Date: Fri, 07 Sep 2007 13:50:16 +0200
From: "Ekki Plicht (DF4OR)" <df4or@web.de>
Subject: Re: Reduce CPU time while polling serial port
Message-Id: <fbrdto$rl8$03$2@news.t-online.com>
jis wrote:
> Hi,
>
> I am using Win32::serialport for reading a data through a scanner
> which is connected to the serial port.
> I use polling as below.But this consumes 99% of my CPU time. and
> slows down the system.
> while(!($data=~/\r/))
> {
> $data=$Scanner->input(); #read the scanner port
> $labeldata=$labeldata.$data; #append
> }
>
>
> Is there any way I implement interrupts or events using perl. Or is
> there any other method to solve this issue.
> I use WIndows NT4(its old..but i have to...)
Set the avaiblabe parameters of [Device|Win32]::SerialPort correctly:
my $ser = Device::SerialPort->new ($conf{dev}, 0, '') || die "Can open
$conf{dev}: $!";
[...]
$ser->read_const_time(2000); # important for nice behaviour, otherwise
hogs cpu
$ser->read_char_time(2000); # dto.
Rgds,
Ekki
------------------------------
Date: Fri, 07 Sep 2007 05:28:56 -0700
From: "fritz-bayer@web.de" <fritz-bayer@web.de>
Subject: Regular Expression
Message-Id: <1189168136.843744.278460@k79g2000hse.googlegroups.com>
Hi,
I 'm looking for a regular expression, which will find a certain word
in a text and replace it, if and only if it does not appear inside an
a html link or inside a tag, for example as an attribute or tag name.
So, for example the following text should not match and be replaced:
<a href='/index.html'>WORD TO MATCH</a> ....
<image alt='WORD TO MATCH' src='../image.gif'> ..
but the following should be replaced
<body><h1>WORD TO MATCH</h1>...
I guess I would have to use a positive lookahead or lookaround
construct to achieve this. I have tried, but could not come up with
anything that will do the job.
Can some pro help me out?
Fritz
------------------------------
Date: Fri, 07 Sep 2007 07:41:30 -0700
From: Klaus <klaus03@gmail.com>
Subject: Re: Regular Expression
Message-Id: <1189176090.215002.220860@22g2000hsm.googlegroups.com>
On Sep 7, 2:28 pm, "fritz-ba...@web.de" <fritz-ba...@web.de> wrote:
> I 'm looking for a regular expression, which will find a certain word
> in a text and replace it, if and only if it does not appear inside an
> a html link or inside a tag
see Perlfaq 4 - How do I find matching/nesting anything?
==================================
This isn't something that can be done in one regular expression, no
matter how complicated. To find something between two single
characters, a pattern like /x([^x]*)x/ will get the intervening bits
in $1. For multiple ones, then something more like /alpha(.*?)omega/
would be needed. But none of these deals with nested patterns. For
balanced expressions using (, {, [ or < as delimiters, use the CPAN
module Regexp::Common, or see (??{ code }) in the perlre manpage. For
other cases, you'll have to write a parser.
If you are serious about writing a parser, there are a number of
modules or oddities that will make your life a lot easier. There are
the CPAN modules Parse::RecDescent, Parse::Yapp, and Text::Balanced;
and the byacc program. Starting from perl 5.8 the Text::Balanced is
part of the standard distribution.
One simple destructive, inside-out approach that you might try is to
pull out the smallest nesting parts one at a time:
while (s/BEGIN((?:(?!BEGIN)(?!END).)*)END//gs) {
# do something with $1
}
A more complicated and sneaky approach is to make Perl's regular
expression engine do it for you. This is courtesy Dean Inada, and
rather has the nature of an Obfuscated Perl Contest entry, but it
really does work:
# $_ contains the string to parse
# BEGIN and END are the opening and closing markers for the
# nested text.
@( = ('(','');
@) = (')','');
($re=$_)=~s/((BEGIN)|(END)|.)/$)[!$3]\Q$1\E$([!$2]/gs;
@$ = (eval{/$re/},$@!~/unmatched/i);
print join("\n",@$[0..$#$]) if( $$[-1] );
==================================
--
Klaus
------------------------------
Date: Fri, 07 Sep 2007 07:43:23 -0700
From: Benoit Lefebvre <benoit.lefebvre@gmail.com>
Subject: Re: Regular Expression
Message-Id: <1189176203.215644.142970@57g2000hsv.googlegroups.com>
On Sep 7, 8:28 am, "fritz-ba...@web.de" <fritz-ba...@web.de> wrote:
> Hi,
>
> I 'm looking for a regular expression, which will find a certain word
> in a text and replace it, if and only if it does not appear inside an
> a html link or inside a tag, for example as an attribute or tag name.
>
> So, for example the following text should not match and be replaced:
>
> <a href='/index.html'>WORD TO MATCH</a> ....
> <image alt='WORD TO MATCH' src='../image.gif'> ..
>
> but the following should be replaced
>
> <body><h1>WORD TO MATCH</h1>...
>
> I guess I would have to use a positive lookahead or lookaround
> construct to achieve this. I have tried, but could not come up with
> anything that will do the job.
>
> Can some pro help me out?
>
> Fritz
I'm sure there is some WAY BETTER WAY to do this..
But here is a solutions that seems to work.
----------------8<--------------------------------------
#!/usr/bin/perl -w
use strict;
my $to_replace = "WORD";
my $replacement = "BLEH";
my @list = ("<a href='/index.html'>WORD</a> ....",
"<image alt='WORD' src='../image.gif'> ..",
"<body><h1>this is my WORD !</h1>... ");
foreach my $line (@list) {
if ($line =~ m/>([^<]*$to_replace[^>]*)</) {
my $match = $1;
$match =~ s/$to_replace/$replacement/g;
$line =~ s/>([^<]*$to_replace[^>]*)</>$match</g;
}
print $line . "\n";
}
--------------------------------------------------------
output:
<a href='/index.html'>BLEH</a> ....
<image alt='WORD' src='../image.gif'> ..
<body><h1>this is my BLEH !</h1>...
------------------------------
Date: Fri, 07 Sep 2007 07:51:45 -0700
From: "fritz-bayer@web.de" <fritz-bayer@web.de>
Subject: Re: Regular Expression
Message-Id: <1189176705.040799.138300@50g2000hsm.googlegroups.com>
On 7 Sep., 17:41, Klaus <klau...@gmail.com> wrote:
> On Sep 7, 2:28 pm, "fritz-ba...@web.de" <fritz-ba...@web.de> wrote:
>
> > I 'm looking for a regular expression, which will find a certain word
> > in a text and replace it, if and only if it does not appear inside an
> > a html link or inside a tag
>
> see Perlfaq 4 - How do I find matching/nesting anything?
>
> ==================================
> This isn't something that can be done in one regular expression, no
> matter how complicated. To find something between two single
> characters, a pattern like /x([^x]*)x/ will get the intervening bits
> in $1. For multiple ones, then something more like /alpha(.*?)omega/
> would be needed. But none of these deals with nested patterns. For
> balanced expressions using (, {, [ or < as delimiters, use the CPAN
> module Regexp::Common, or see (??{ code }) in the perlre manpage. For
> other cases, you'll have to write a parser.
>
> If you are serious about writing a parser, there are a number of
> modules or oddities that will make your life a lot easier. There are
> the CPAN modules Parse::RecDescent, Parse::Yapp, and Text::Balanced;
> and the byacc program. Starting from perl 5.8 the Text::Balanced is
> part of the standard distribution.
>
> One simple destructive, inside-out approach that you might try is to
> pull out the smallest nesting parts one at a time:
>
> while (s/BEGIN((?:(?!BEGIN)(?!END).)*)END//gs) {
> # do something with $1
> }
>
> A more complicated and sneaky approach is to make Perl's regular
> expression engine do it for you. This is courtesy Dean Inada, and
> rather has the nature of an Obfuscated Perl Contest entry, but it
> really does work:
>
> # $_ contains the string to parse
> # BEGIN and END are the opening and closing markers for the
> # nested text.
>
> @( = ('(','');
> @) = (')','');
> ($re=$_)=~s/((BEGIN)|(END)|.)/$)[!$3]\Q$1\E$([!$2]/gs;
> @$ = (eval{/$re/},$@!~/unmatched/i);
> print join("\n",@$[0..$#$]) if( $$[-1] );
> ==================================
>
> --
> Klaus
Well, I would know if it's possible, but positive and negative
lookaheads seem to be something to consider. The following shows how:
http://frank.vanpuffelen.net/2007/04/how-to-optimize-regular-expression.html
------------------------------
Date: Fri, 07 Sep 2007 07:41:55 -0700
From: lerameur <lerameur@yahoo.com>
Subject: Switching with case in perl ?
Message-Id: <1189176115.853558.8120@r29g2000hsg.googlegroups.com>
Hello,
I used the switching command CASE in c++
Is there such a thing in perl.? I did not find the command from
googling with perl.
the reason for this is I am trying to increment a time. Maybe there is
a way around it. Here is the part of the program I have:
Goal: there are many log files per hour. The program should put it to
one file, starting from the user input date. The problem is getting
the roller over date.
$year2= 07; #type in the year
$month2= 09; #type in the month
$day2= 05; #type in the day
$hours2 = 04; #type in the hour
$logTime = $year$month$day$hours; # date to begin search
$minutes_count = 0; #Put to zero every time it will go throw the loop
to gather all log files within that hour
if($minutes_count<10) {
$minutes_count="0$minutes_count";
} else {
$minutes_count=$minutes_count; #Calculate
Minutes_count for double digit
}
$logTimeIncrement = $logTime$minutes_count"*";
while ($minutes_count != 60){ # This loop goes through every minute
for that our and append the new found log to the main hour log
if ($logTimeIncrement == true){
open (log_$logTime, >>log_$logTime) ||
die ("Cannot open input file $logTimeIncrement \n") ; # this file is
the accumulator file
open (File, >>$logTimeIncrement) ||
die ("Cannot open input file $logTimeIncrement \n") ;
$doc1 = <log_$logTime>;
$doc2 = <File>;
while ($doc1 ne " " || $doc2 ne " "){
if ($doc1 ne " ") {
print ($doc1);
$doc1 = <log_$logTime>>;
}
if ($doc2 ne " "){
print ($doc2);
$doc2 = <File>;
}
} #end of while loop
}
$minutes_count++;
if($minutes_count<10) {
$minutes_count="0$minutes_count";
} else {
$minutes_count=$minutes_count;
}
# Checking the roller over dates
if ($minutes_count ==60){
$hours2++;
$minutes_count=0;
}
if ($hours2==24){
$day2++;
$hours2=0;
}
I stopped here since I decided to look for case equivalent.
------------------------------
Date: Fri, 07 Sep 2007 08:04:39 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Switching with case in perl ?
Message-Id: <1189177479.157040.223890@r34g2000hsd.googlegroups.com>
On Sep 7, 10:41 am, lerameur <leram...@yahoo.com> wrote:
> I used the switching command CASE in c++
> Is there such a thing in perl.?
Yes. perldoc Switch.
> I did not find the command from googling with perl.
Google should be your last resort. The built in documentation should
be your first.
$ perldoc -q switch
Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq7.pod
How do I create a switch or case statement?
> the reason for this is I am trying to increment a time. Maybe
> there is a way around it. Here is the part of the program I
> have:
> Goal: there are many log files per hour. The program should put
> it to one file, starting from the user input date. The problem is
> getting the roller over date.
>
> $year2= 07; #type in the year
> $month2= 09; #type in the month
> $day2= 05; #type in the day
> $hours2 = 04; #type in the hour
That can't be your real code. You're using octal numbers, and 9 isn't
a valid octal digit. This causes a syntax error. Please copy and
paste your real code from now on.
> $logTime = $year$month$day$hours; # date to begin search
More syntax errors.
> $minutes_count = 0; #Put to zero every time it will go throw the loop
> to gather all log files within that hour
> if($minutes_count<10) {
> $minutes_count="0$minutes_count";
> } else {
> $minutes_count=$minutes_count; #Calculate
> Minutes_count for double digit
> }
This makes no sense. At the time of this if statement, $minutes_count
will always be zero. So why don't you just start it off at "00" to
begin with?
> $logTimeIncrement = $logTime$minutes_count"*";
More syntax errors.
> while ($minutes_count != 60){ # This loop goes through every minute
> for that our and append the new found log to the main hour log
> if ($logTimeIncrement == true){
Not a syntax error, but definitely not what you want. The bareword
true will either be seen as a function call true() or as the string
"true". The string "true" in numeric context is 0, which is false.
I'm done looking through this code. If you want to increment time
values, you should be using the built in time manipulation functions:
use POSIX 'strftime';
my $year = 7;
my $month = 9;
my $day = 5;
my $hours = 4;
for my $minute (45..70) {
print strftime("%H:%M %m/%d/%Y", 0, $minute, $hours, $day, $month -
1, $year + 100), "\n"
}
Output:
04:45 09/05/2007
04:46 09/05/2007
04:47 09/05/2007
04:48 09/05/2007
04:49 09/05/2007
04:50 09/05/2007
04:51 09/05/2007
04:52 09/05/2007
04:53 09/05/2007
04:54 09/05/2007
04:55 09/05/2007
04:56 09/05/2007
04:57 09/05/2007
04:58 09/05/2007
04:59 09/05/2007
05:00 09/05/2007
05:01 09/05/2007
05:02 09/05/2007
05:03 09/05/2007
05:04 09/05/2007
05:05 09/05/2007
05:06 09/05/2007
05:07 09/05/2007
05:08 09/05/2007
05:09 09/05/2007
05:10 09/05/2007
Paul Lalli
------------------------------
Date: Fri, 07 Sep 2007 05:27:12 -0500
From: "Mumia W." <paduille.4061.mumia.w+nospam@earthlink.net>
Subject: Re: trying to export some utility methods, and struggling
Message-Id: <13e2acbf6itlnd8@corp.supernews.com>
On 09/07/2007 03:00 AM, bugbear wrote:
> [...]
> I also want the module to make some utility
> methods and variables available; I would
> like these to be avaiable (nearly) globally,
> in that they are of use in the implementation of WidgetBasher,
> the implementation of an WidgetManip sub-class,
> and of great use to anybody (the user) working
> on Widgets.
> [...]
Although it's not illegal to export functions from OO modules in Perl,
it's not such a great idea because you have a much more powerful system
for sharing code if you're using objects.
I suggest abandoning the Exporter and just making the utility routines
class methods, e.g.:
#!/usr/bin/perl
package WidgetBasher;
use strict;
use warnings;
sub utility_function {
my $class = shift;
print "Utility function called (from $class)\n";
}
package WidgetManip;
use base 'WidgetBasher';
package WidgetDoer;
use base 'WidgetBasher';
package main;
# Let's say we're back in the main program now.
WidgetBasher->utility_function();
WidgetManip->utility_function();
WidgetDoer->utility_function();
__HTH__
------------------------------
Date: Fri, 07 Sep 2007 12:27:14 +0100
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: trying to export some utility methods, and struggling
Message-Id: <13e2dcjgu1h4beb@corp.supernews.com>
(reponse at bottom)
Mumia W. wrote:
> On 09/07/2007 03:00 AM, bugbear wrote:
>> [...]
>> I also want the module to make some utility
>> methods and variables available; I would
>> like these to be avaiable (nearly) globally,
>> in that they are of use in the implementation of WidgetBasher,
>> the implementation of an WidgetManip sub-class,
>> and of great use to anybody (the user) working
>> on Widgets.
>> [...]
>
> Although it's not illegal to export functions from OO modules in Perl,
> it's not such a great idea because you have a much more powerful system
> for sharing code if you're using objects.
More powerful, agreed, but in some cases less convenient,
or more verbose.
> I suggest abandoning the Exporter and just making the utility routines
> class methods, e.g.:
>
> #!/usr/bin/perl
> package WidgetBasher;
> use strict;
> use warnings;
>
> sub utility_function {
> my $class = shift;
> print "Utility function called (from $class)\n";
> }
>
> package WidgetManip;
> use base 'WidgetBasher';
No; WidgetManips should not extend WidgetBashers.
Think of WidgetBasher as a food processor,
and WidgetManips as optional attachements.
A "manip" is not a "basher" (or vise versa)
I quite possibly didn't make this clear.
>
> package WidgetDoer;
> use base 'WidgetBasher';
>
> package main;
>
> # Let's say we're back in the main program now.
>
> WidgetBasher->utility_function();
> WidgetManip->utility_function();
> WidgetDoer->utility_function();
Yep, that works, but that's almost exactly what I'm trying to avoid.
I don't want to be forced to have an instance of a class
just to call a utility;
I am aware of the various uses of OO code (I've spent 15 years
on large projects in both Java and C++), but this is not a large
project.
BugBear
------------------------------
Date: Fri, 07 Sep 2007 08:03:39 -0700
From: lerameur <lerameur@yahoo.com>
Subject: variable running once
Message-Id: <1189177419.329650.19010@o80g2000hse.googlegroups.com>
Hello
I just wrote a small script. I would like to know if the following is
possible:
I want to have some initial values like a time in the script. This
script is called from crontab every hour. These initial values should
be run only once when the script starts. Is there such a command in
perl ?
I thought of putting theses values in a separate file and when the
perl program starts, fetches those values and when it exist saves the
newest value so when it is called again it continues where it has
left off.
thanks
k
------------------------------
Date: Fri, 7 Sep 2007 16:54:26 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Warning about unused lexical variables
Message-Id: <slrnfe2ph2.se.hjp-usenet2@zeno.hjp.at>
On 2007-09-06 18:58, Jürgen Exner <jurgenex@hotmail.com> wrote:
> Peter J. Holzer wrote:
>> It would be nice if perl could warn about lexical variables which are
>> never used in their scope after their initialization. Does anybody
>> else find this useful, and if so, is there a reason (besides "life is
>> short") why it hasn't been implemented?
>
> Useful? Yes.
> Easy to implement? No.
I don't think it would be particularly hard to implement. That's a
rather common feature in compilers which generate machine code (IIRC
it's a byproduct of register allocation). But I don't know how expensive
it is - since perl compiles the source code every time it is run, we
want to avoid algorithms which can potentially take a long time.
> Example:
>
> my ($x, $y) = (1,2);
> if (<some complex condition depending on the environment>) {
> call_my_sub($x)
> } else {
> call_my_sub($y)
> }
>
> In this example either $x or $y remains unused.
In any particular run, yes. But both branches are possible, so you can
eliminate neither $x nor $y. So that code is ok, although it would
probably be cleaner to rearrange it as:
if (<some complex condition depending on the environment>) {
my $x = 1;
call_my_sub($x)
} else {
my $y = 2;
call_my_sub($y)
}
hp
--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | hjp@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
------------------------------
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 829
**************************************