Home > Archive > PERL Beginners > October 2006 > problem with eval
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| magdafrog@yahoo.de 2006-10-02, 6:59 pm |
| hi,
i have described my problems in comments.
use File::Path;
$_[0] =~ /(.){1}/;
my $mail_dir = '/var/spool/cyrus/mail/'.$1.'/user/'.$_[0];
my @dirs= ($mail_dir, $mail_dir.'/Ham', $mail_dir.'Spam');
File::Path::mkpath( \@dirs , 0, 0755);
eval ($mail_dir);
if($@)
{
########################################
#
#dirs are created, but in /tmp/ui,log im getting
#syntax error at (eval 50) line 1, near "/var/spool"
#
########################################
#
open (LOG, ">>/tmp/um.log");
print LOG "error: couldn't create dir: $mail_dir\n$@";
close LOG;
}
could someone help??
thanx in advance
many greetings
magda muskala
| |
| Paul Lalli 2006-10-02, 6:59 pm |
| magdafrog@yahoo.de wrote:
> i have described my problems in comments.
>
> use File::Path;
> $_[0] =~ /(.){1}/;
What is in the @_ array? Generally, only subroutine arguments go to
this array. Is this code inside a subroutine? Please post a short but
COMPLETE script for best assistance.
NEVER use the $1, $2, $3, etc variables without first checking to makes
sure the pattern match was successful.
The {1} quantifier is a no-op, always, in every regexp ever. There is
never a need for it.
if ($_[0] =~ /(.)/) {
> my $mail_dir = '/var/spool/cyrus/mail/'.$1.'/user/'.$_[0];
Learn to love interpolation.
my $mail_dir = "/var/spool/cyrus/mail/$1/user/$_[0]";
> my @dirs= ($mail_dir, $mail_dir.'/Ham', $mail_dir.'Spam');
Why does Ham have a preceding slash, but Spam does not?
> File::Path::mkpath( \@dirs , 0, 0755);
This statement creates those three directories.
> eval ($mail_dir);
What is it you think this command should be doing?
> if($@)
> {
>
> ########################################
#
> #dirs are created, but in /tmp/ui,log im getting
> #syntax error at (eval 50) line 1, near "/var/spool"
> ########################################
#
I don't think you understand what eval() does. It takes a string and
evaluates that string as *Perl* code. It does not attempt to execute a
system command. Your eval is the equivalent of just typing this line
into your Perl source:
/var/spool/cyrus/mail/$1/user/$_[0];
Obviously, that is not valid Perl syntax.
It's not possible to tell from your code, but maybe you want one of
system(), exec(), or back-ticks?
perldoc -f system
perldoc -f exec
perldoc perlop
> could someone help??
People can help you *much* more easily and better if you are explicit
about :
* your actual end goal
* your input
* your desired output
* your actual output - including EXACT TEXT of error messages
* short, complete, *runnable* code that demonstrates the problem.
In this post, I've had to make several guesses and assumptions, and so
the quality of my response might not be completely helpful.
Paul Lalli
| |
| magdafrog@yahoo.de 2006-10-02, 6:59 pm |
| Paul Lalli schrieb:
hi,
thanx for your answer.
>
> Why does Ham have a preceding slash, but Spam does not?
yeah, was a typo.
> I don't think you understand what eval() does. It
> takes a string and
>
yep, have written junk there... stupid /me :os i
meant.
eval{mkpath($dir)}
>
>
>
ok, have one more problem...
im gonna be more precise now.
im pasting the routine again and present my problem
in a comment
########################################
#im calling the routine with 'test' as an argument
#######################################
sub generate_mailbox($)
{
use File::Path;
$_[0] =~ /(.){1}/;
my $mail_dir = '/var/spool/cyrus/mail/'.$1.'/user/'.$_[0];
my @dirs= ($mail_dir, $mail_dir.'/Ham',$mail_dir.'/Spam');
eval{File::Path::mkpath( \@dirs , 0, 0755)};
if($@)
{
########################################
###
#this would priunt me an exception(if any),
#but there is no exception, so the program goes on
########################################
###
throw Bitkit::Exception($@);
}
else
{
#system('chown','-R','cyrus:mail', $mail_dir);
#chown ('cyrus', 'mail', @dir) ;
######################################
#now i can see this dir in a shell
#
#frog:~# ls /var/spool/cyrus/mail/t/user/test
#frog:~# Spam Ham
#
#but the rights are not changed...
########################################
chown ('cyrus', 'mail', $mail_dir) ;
if($!){
##################################
#but here the exception is catched:
# No such fiile or directory:
# /var/spool/cyrus/mail/t/user/test
##################################
throw Bitkit::Exception($! ,$mail_dir);
}
}
any hints??
thanx in advance,
greetings
| |
| Paul Lalli 2006-10-02, 6:59 pm |
| magdafrog@yahoo.de wrote:
> #im calling the routine with 'test' as an argument
>
> #######################################
> sub generate_mailbox($)
You really shouldn't use prototypes. Ever, really. Search this group
for multiple reasons why.
> {
> use File::Path;
> $_[0] =~ /(.){1}/;
So you didn't bother following any of the advice I gave in the previous
post that wasn't directly related to answering your question? Sigh.
> my $mail_dir = '/var/spool/cyrus/mail/'.$1.'/user/'.$_[0];
> my @dirs= ($mail_dir, $mail_dir.'/Ham',$mail_dir.'/Spam');
> eval{File::Path::mkpath( \@dirs , 0, 0755)};
> if($@)
> {
>
> ########################################
###
> #this would priunt me an exception(if any),
> #but there is no exception, so the program goes on
> ########################################
###
> throw Bitkit::Exception($@);
> }
> else
> {
> #system('chown','-R','cyrus:mail', $mail_dir);
> #chown ('cyrus', 'mail', @dir) ;
> ######################################
> #now i can see this dir in a shell
> #
> #frog:~# ls /var/spool/cyrus/mail/t/user/test
> #frog:~# Spam Ham
> #
> #but the rights are not changed...
> ########################################
> chown ('cyrus', 'mail', $mail_dir) ;
$ perldoc -f chown
chown LIST
Changes the owner (and group) of a list of files.
The first two elements of the list must be the
*numeric* uid and gid, in that order.
You probably want to use the getpwuid and its family of functions
there...
> if($!){
This is not a valid method of checking for error. A successful system
call does not reset the $! variable. Indeed, the value of $! cannot be
counted upon to be anything specific after a successful system call.
Instead, check the return value of chown(). If it didn't change the
number of files you were expecting it to, *then* you have an error.
Paul Lalli
| |
| magdafrog@yahoo.de 2006-10-03, 8:02 am |
| > You really shouldn't use prototypes. Ever, really. Search this group
> for multiple reasons why.
made a quick search & foun the example with $@ as an argument...
but the function call was wrong...
imho if i declare $ and @ as arguments i shouldn' t call this function
with only @...
gonna search more...
cause you told, there are multiple di ventages.
>
> So you didn't bother following any of the advice I gave in the previous
> post that wasn't directly related to answering your question? Sigh.
it is kind of you, that you gave me also the other hints.
changed it also, just was in a hurry yesterdy and pasted the ugly
version.
[color=darkred]
yep, now works
..
thank you very much
greetings
magda
| |
| Paul Lalli 2006-10-03, 8:02 am |
| magdafrog@yahoo.de wrote:
> made a quick search & foun the example with $@ as an argument...
> but the function call was wrong...
> imho if i declare $ and @ as arguments i shouldn' t call this function
> with only @...
Say your prototype is:
sub fctn($);
What do you suppose happens if you accidentally call this with an
array?
fcnt(@stuff);
If you think Perl will give you an error, you're very wrong. What
actually happens is that the *size* of the array gets passed into the
function, silently. Perl doesn't tell you this happens. It assumes
that's what you wanted.
Say your prototype is:
sub fctn($@);
meaning that you want to call it with a scalar and a list. Do you
think this will give you an error?
fctn("Foobar");
It won't. Because as far as Perl is concerned, and empty list is still
a list.
Prototypes create a false sense of security, making you think you don't
have to check your argument list manually. They silently change your
arguments, passing different values than you think you're passing it,
without giving any errors.
They are far more trouble than they're worth.
Paul Lalli
|
|
|
|
|