2

I'm trying to find the last access date of clearcase view, perl script looks like below.

    @Property = `cleartool lsview -prop $viewtag ` ;

foreach $property (@Property)
    {
    $last_accessed = $property if ( $property =~ /^Last accessed / ); 
            # | cut  -b 15-24 | awk -F '-' '{ print $3"/"$2"/"$1 }'
    }

Problem what i'm facing is perl script exit if cleartool command fails. I want perl to continue even though cleartool returns error.

BRs Mani.

maestromani
  • 841
  • 1
  • 9
  • 31

2 Answers2

8

The simple and primitive way is to put the potentially failing code inside an eval block:

eval { @Property = `cleartool lsview -prop $viewtag ` };

That way your Perl script will continue even if cleartool fails.

The correct way is to use an appropriate module like Try::Tiny. The error will be available inside the catch block in the variable $_.

try {
    @Property = `cleartool lsview -prop $viewtag `;
}
catch {
    warn "cleartool command failed with $_";
};
Perleone
  • 3,958
  • 1
  • 26
  • 26
3

You can try and use "Try::Tiny", as recommended in "What is the best way to handle exceptions in perl?".

The other approach is to use eval the cleartool command.

eval { @Property = `cleartool lsview -prop $viewtag` };
if ($@) {
    warn "Oh no! [$@]\n";
}
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250