perlnewbieak has asked for the wisdom of the Perl Monks concerning the following question:

Hello Kennethk,ercparker and gulden,
Thanq very much for the quick reply.
Sorry I didnt post you the entire code before.
Here I posted the entire code.
Gulden I tried with FindBin also but the problem is
when we use Findbin we cant use $0,(where I need filename along with the path)
So when I am printing the $path I am getting the path in the console.
But while I insert it to the "DBtst" it is not giving the path like this
D:\Portal\Types\Program\Script_types.pl
instead giving the path as D:PortalTypesProgramScript_types.pl
Please find a solution to my problem
Thanku
use Config::Simple; use warnings; $cfg = new Config::Simple(); $cfg->read('../config/Amis_a.conf'); use Cwd qw(abs_path); $dbh = DBI->connect('dbi:mysql:' . $DB_schema . ';' . $DB_server , $DB +_login , $DB_password) or die "Connection Error: $DBI::errstr\n"; my $status; my $message; open(MYFILE, "<err_output_types.txt"); # open for input flock(MYFILE, LOCK_EX); seek(MYFILE, 0, SEEK_SET); my(@lines) = <MYFILE>; # read file into list my $size = @lines; print "Size $size\n"; foreach $line (@lines) { print "$line"; } close(MYFILE); if ($size !=0) { $status=ERROR; $message="Program failed due to Errors"; } else { $status=OK; $message="Program ended successfully"; } my $path = abs_path($0); print "$path\n"; print $message; my $sth = $dbh->prepare(<<SQL); INSERT IGNORE INTO DBtst.logs(Status,Message,Origin)VALUES('$status',' +$message','$path') ; SQL $sth->execute() or die $DBI::errstr;

Replies are listed 'Best First'.
Re: find the path of perl script
by kennethk (Abbot) on Dec 17, 2009 at 17:24 UTC
    I cannot replicate your reported issue with the posted code. Specifically, for your case, running:

    #!/usr/bin/perl use strict; use warnings; use Cwd qw(abs_path); my $path = abs_path($0); print "$path\n";

    would return

    D:/Portal/Types/Program/Script_types.pl

    Please test the above code on your machine so we can try to replicate your issue and determine where there is a disconnect. If the above works, please post a minimally complete script that replicates your issue. See How do I post a question effectively?. As well, please wrap code in <code> tags to aid in download - see Writeup Formatting Tips.

Re: find the path of perl script
by ercparker (Hermit) on Dec 17, 2009 at 17:53 UTC

    I tested the following code on Windows XP using with Perl 5.8.8

    #!C:\Program Files\Perl\bin\perl.exe use strict; use warnings; use Cwd qw(abs_path); my $path = abs_path($0); print "$path\n"; exit(1);

    It printed in the expected format: D:\Portal\Types\Program\Script_types.pl

    Sorry I couldn't be of more help.

Re: find the path of perl script
by gulden (Monk) on Dec 17, 2009 at 18:00 UTC
    There's another perl module for that: FindBin.
    use FindBin qw($Bin); print $Bin;
    «A contentious debate is always associated with a lack of valid arguments.»

      I would go with core module File::Basename

      use strict; use warnings; use File::Basename; my($filename, $directories, $suffix) = fileparse($0, qr/\.[^.]*/); print $filename,"\n";
Re: find the path of perl script
by ercparker (Hermit) on Dec 18, 2009 at 22:01 UTC

    Backslash '\' is a special character. try this before you insert.

    $path = $dbh->quote($path);

    You should also look into using placeholders.

    I hope that helps.

      Hmm, nice idea, but using placeholders is better. See also Re^2: Massive Memory Leak.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)