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

Hello Monks, I have a file name and path which i am trying to parse. The $file contains the full path of the file.So i split based on \. Taking the second argument(file resides in a folder under root), i want to strip the extension. This is what I am doin.

foreach my $file (@files){ my @arr=split(/\//,$file); print "\n $arr[2];"; }

Now this is what I am trying to do to remove the extension for the file. "system%abc%123%.txt" which is the valule in $arr2.

my $length_arr=length($arr[2]); my $fname=sub($arr[2],0,($length_arr-4)); print $fname;

It gives me a weird error."Perl Command Line Interpreter has encountered a problem and needs to close.We are sorry for the inconvinience.

The error signature looks like this. AppName:perl.exe AppVer:5.6.1.635 ModName:perl56.dll ModVer: 5.6.1.635 Offset: 0001c6d9 Can anyone help me figure what could be wrong? Thanks Sandy (smanicka) Sorry, I feel soo stupid.Its substr as pointed out by ysth.Thanks

Replies are listed 'Best First'.
Re: weird error when using substring function
by ysth (Canon) on Dec 22, 2008 at 19:08 UTC
Re: weird error when using substring function
by ikegami (Patriarch) on Dec 22, 2008 at 19:12 UTC

    You used sub instead of substr

    If that doesn't fix it, you'll need to try your code in a newer version of Perl. You're getting an internal error, so it's either been fixed in newer version, or needs to be fixed in newer versions. It won't get fixed in 5.6.x.

    I can't get 5.6.1 to crash as you describe using the bad or the fixed code.

Re: weird error when using substring function
by Tanktalus (Canon) on Dec 22, 2008 at 19:06 UTC

    Have you checked File::Basename or File::Spec's splitpath function? One, or both, of these, will do what you're looking for (though I'm not sure about the crash - maybe upgrading to a newer version of perl would help...)

Re: weird error when using substring function
by gube (Parson) on Dec 23, 2008 at 00:43 UTC
    Use File::Basename module it will be safe always find one liner code perl -MFile::Basename -e'print basename ("/home/gubendran/Opportunity.sql")'